Advertisement
Guest User

Untitled

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