Advertisement
Guest User

Untitled

a guest
Aug 17th, 2010
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. /*
  2. * Credits go to LeTama for the data connection code and phh for the wrapper code
  3. * Copyright (C) 2010 Sebastian Heinecke (gauner1986)
  4. */
  5.  
  6. #include <telephony/ril.h>
  7. #include <dlfcn.h>
  8.  
  9. #include <utils/Log.h>
  10. #include <stdio.h>
  11. #include <assert.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include <pthread.h>
  19. #include <alloca.h>
  20. #include "atchannel.h"
  21. #include "at_tok.h"
  22. #include "misc.h"
  23. #include <getopt.h>
  24. #include <sys/socket.h>
  25. #include <cutils/sockets.h>
  26. #include <termios.h>
  27. #include <utils/Log.h>
  28.  
  29. #define LOG_TAG "RILW"
  30.  
  31. #define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
  32.  
  33. static const struct RIL_Env *s_rilenv;
  34.  
  35. static void *ril_handler=NULL;
  36.  
  37. int hackWrite (int s_fd, const char *s)
  38. {
  39. size_t cur = 0;
  40. size_t len = strlen(s);
  41. ssize_t written;
  42.  
  43. LOGD("AT> %s\n", s);
  44.  
  45. /* the main string */
  46. while (cur < len) {
  47. do {
  48. written = write (s_fd, s + cur, len - cur);
  49. } while (written < 0 && errno == EINTR);
  50.  
  51. if (written < 0) {
  52. return AT_ERROR_GENERIC;
  53. }
  54.  
  55. cur += written;
  56. }
  57.  
  58. /* the \r */
  59.  
  60. do {
  61. written = write (s_fd, "\r" , 1);
  62. } while ((written < 0 && errno == EINTR) || (written == 0));
  63.  
  64. if (written < 0) {
  65. return AT_ERROR_GENERIC;
  66. }
  67.  
  68. return 0;
  69. }
  70.  
  71.  
  72. void hackDeactivateData(void *data, size_t datalen, RIL_Token t)
  73. {
  74. int err;
  75. char * cmd;
  76. char * cid;
  77. int fd,fd_smd,i,fd2;
  78. ATResponse *p_response = NULL;
  79. struct termios ios;
  80.  
  81. LOGD("PL: hackDeactivateData: starting\n");
  82. fd_smd = open ("/dev/smd0", O_RDWR);
  83.  
  84. if(fd_smd == -1) {
  85. LOGW("PL:hackDeactivateData: Error opening smd0\n");
  86. }
  87. tcgetattr( fd_smd, &ios );
  88. ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
  89. tcsetattr( fd_smd, TCSANOW, &ios );
  90.  
  91.  
  92.  
  93. cid = ((char **)data)[0];
  94. asprintf(&cmd, "AT+CGACT=0,%s", cid);
  95. err = hackWrite(fd_smd, cmd);
  96. free(cmd);
  97. close(fd_smd);
  98.  
  99. LOGW("PL:hackDeactivateData: killing pppd\n");
  100.  
  101. i=0;
  102. while((fd = open("/etc/ppp/ppp0.pid",O_RDONLY)) > 0) {
  103. if(i%5 == 0) {
  104. system("killall pppd");
  105. }
  106. close(fd);
  107. if(i>25)
  108. goto error;
  109. i++;
  110. sleep(1);
  111. }
  112. RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
  113. return;
  114.  
  115. error:
  116. RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
  117. }
  118.  
  119. void hackSetupData(char **data, size_t datalen, RIL_Token t)
  120. {
  121. const char *apn;
  122. char *user = NULL;
  123. char *pass = NULL;
  124. char *cmd;
  125. char *userpass;
  126. int err;
  127. ATResponse *p_response = NULL;
  128. int fd, pppstatus,i,fd2;
  129. FILE *pppconfig;
  130. size_t cur = 0;
  131. ssize_t written, rlen;
  132. char status[32] = {0};
  133. char *buffer;
  134. long buffSize, len;
  135. int retry = 10;
  136. char *response[3] = { "1", "ppp0", "255.255.255.255" };
  137. int mypppstatus;
  138. int ret;
  139. struct termios ios;
  140.  
  141. LOGW("PL:hackSetupData\n");
  142.  
  143. fd = open ("/dev/smd0", O_RDWR);
  144.  
  145. if(fd == -1) {
  146. LOGW("PL:hackSetupData: Error opening smd0\n");
  147. }
  148. tcgetattr( fd, &ios );
  149. ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
  150. tcsetattr( fd, TCSANOW, &ios );
  151.  
  152.  
  153. // ret = at_open(fd, onHackUnsolli);
  154.  
  155. apn = ((const char **)data)[2];
  156. user = ((char **)data)[3];
  157. if(user != NULL)
  158. {
  159. if (strlen(user)<2)
  160. user = "dummy";
  161. } else
  162. user = "dummy";
  163.  
  164. pass = ((char **)data)[4];
  165. if(pass != NULL)
  166. {
  167. if (strlen(pass)<2)
  168. pass = "dummy";
  169. } else
  170. pass = "dummy";
  171. /*
  172. LOGD("requesting data connection to APN '%s'\n", apn);
  173. i=0;
  174. while((fd = open("/etc/ppp/ppp-gprs.pid",O_RDONLY)) > 0) {
  175. if(i%5 == 0) {
  176. fd2 = open("/smodem/control",O_WRONLY);
  177. if(fd2 < 0)
  178. goto error;
  179. write(fd2, "killppp", 7);
  180. close(fd2);
  181. }
  182. close(fd);
  183. if(i>25)
  184. goto error;
  185. i++;
  186. sleep(1);
  187. } */
  188. if(*data[0]=='0')
  189. LOGE("Android want us to connect as CDMA while we are a GSM phone !");
  190.  
  191.  
  192. LOGW("PL:hackSetupData - InitModem\n");
  193.  
  194. LOGW("PL:hackSetupData - AT+GCDCONT\n");
  195. asprintf(&cmd, "AT+CGDCONT=1,\"IP\",\"%s\",,0,0", apn);
  196. //FIXME check for error here
  197. //err = at_send_command(cmd, NULL);
  198. hackWrite(fd, cmd);
  199. free(cmd);
  200. sleep(1);
  201. // Set required QoS params to default
  202. LOGW("PL:hackSetupData - AT+GCDREQ\n");
  203. err = hackWrite(fd, "AT+CGQREQ=1");
  204. sleep(1);
  205. // Set minimum QoS params to default
  206. LOGW("PL:hackSetupData - AT+GCDMIN\n");
  207. err = hackWrite(fd, "AT+CGQMIN=1");
  208. sleep(1);
  209. // packet-domain event reporting
  210. LOGW("PL:hackSetupData - AT+GCEREP\n");
  211. err = hackWrite(fd, "AT+CGEREP=1,0");
  212. sleep(1);
  213. // Hangup anything that's happening there now
  214. LOGW("PL:hackSetupData - AT+CGACT\n");
  215. err = hackWrite(fd, "AT+CGACT=0,1");
  216. sleep(1);
  217. // Start data on PDP context 1
  218. LOGW("PL:hackSetupData - ATD*99\n");
  219. err = hackWrite(fd, "ATD*99***1#");
  220. // if (err < 0 || p_response->success == 0) {
  221. // at_response_free(p_response);
  222. // LOGE("PL: Dial failed\n");
  223. // goto error;
  224. //}
  225. //at_response_free(p_response);
  226. sleep(1);
  227. close(fd);
  228.  
  229. LOGD("PL: hackSetupData: saving secrets\n");
  230. asprintf(&userpass, "%s * %s\n", user, pass);
  231. len = strlen(userpass);
  232. fd = open("/etc/ppp/pap-secrets",O_WRONLY);
  233. if(fd < 0) {
  234. LOGE("PL: unable to open /etc/ppp/pap-secrets\n");
  235. goto error;
  236. }
  237. write(fd, userpass, len);
  238. close(fd);
  239. fd = open("/etc/ppp/chap-secrets",O_WRONLY);
  240. if(fd < 0) {
  241. LOGE("PL: unable to open /etc/ppp/chap-secrets\n");
  242. goto error;
  243. }
  244. write(fd, userpass, len);
  245. close(fd);
  246. free(userpass);
  247.  
  248. LOGD("PL: hackSetupData: saving options\n");
  249. pppconfig = fopen("/etc/ppp/options.smd","r");
  250. if(!pppconfig) {
  251. LOGE("PL: unable to open /etc/ppp/options.smd\n");
  252. goto error;
  253. }
  254.  
  255. //filesize
  256. fseek(pppconfig, 0, SEEK_END);
  257. buffSize = ftell(pppconfig);
  258. rewind(pppconfig);
  259.  
  260. //allocate memory
  261. buffer = (char *) malloc (sizeof(char)*buffSize);
  262. if (buffer == NULL) {
  263. LOGE("PL: hackSetupData: malloc buffer failed\n");
  264. goto error;
  265. }
  266.  
  267. //read in the original file
  268. len = fread (buffer,1,buffSize,pppconfig);
  269. if (len != buffSize) {
  270. LOGE("PL: hackSetupData: fread failed\n");
  271. goto error;
  272. }
  273. fclose(pppconfig);
  274.  
  275. pppconfig = fopen("/etc/ppp/options.smd1","w");
  276. fwrite(buffer,1,buffSize,pppconfig);
  277. fprintf(pppconfig,"user %s\n",user);
  278. fclose(pppconfig);
  279. free(buffer);
  280.  
  281. // The modem replies immediately even if it's not connected!
  282. // so wait a short time.
  283.  
  284. sleep(5);
  285. LOGD("PL: hackSetupData: launching pppd\n");
  286. //plLaunchPPP();
  287. mypppstatus = system("/system/bin/pppd /dev/smd1 defaultroute");//Or smd7 ?
  288. if (mypppstatus < 0) {
  289. LOGE("PL: system(/bin/pppd failed\n");
  290. goto error;
  291. }
  292. else {
  293. LOGD("PL: hackSetupData: system(/bin/pppd returned %d\n", mypppstatus);
  294. }
  295. sleep(5); // allow time for ip-up to run
  296. /*
  297. * We are supposed to return IP address in response[2], but this is not used by android currently
  298. */
  299. /*
  300. inaddr_t addr,mask;
  301. unsigned int flags;
  302. ifc_init();
  303. ifc_get_info(PPP_TTY_PATH, &addr, &mask, &flags);
  304. ifc_close();
  305. */
  306. LOGD("PL:hack normal exit\n");
  307. RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
  308. return;
  309. error:
  310. LOGW("PL:hackSetupData: ERROR ?");
  311. RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
  312. //fixme : cleanup mess
  313. }
  314.  
  315. void (*htc_onRequest)(int request, void *data, size_t datalen, RIL_Token t);
  316. void onRequest(int request, void *data, size_t datalen, RIL_Token t) {
  317. if(request==RIL_REQUEST_SETUP_DATA_CALL){ // Let's have fun !
  318. hackSetupData(data, datalen, t);
  319. return;
  320. } else if(request==RIL_REQUEST_DEACTIVATE_DATA_CALL) {
  321. hackDeactivateData(data, datalen, t);
  322. return;
  323. }
  324. return htc_onRequest(request, data, datalen, t);
  325. }
  326.  
  327. const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv) {
  328. s_rilenv = env;
  329.  
  330. ril_handler=dlopen("/system/lib/libhtc_ril.so", 0/*Need to RTFM, 0 seems fine*/);
  331. RIL_RadioFunctions* (*htc_RIL_Init)(const struct RIL_Env *env, int argc, char **argv);
  332.  
  333. htc_RIL_Init=dlsym(ril_handler, "RIL_Init");
  334. RIL_RadioFunctions *s_callbacks;
  335. s_callbacks=htc_RIL_Init(env, argc, argv);
  336. htc_onRequest=s_callbacks->onRequest;
  337. s_callbacks->onRequest=onRequest;
  338. return s_callbacks;
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement