luat_mobile_common.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_mobile.h"
  4. #include "luat_rtos.h"
  5. #include "luat_fs.h"
  6. #if defined(LUAT_EC7XX_CSDK) || defined(CHIP_EC618) || defined(__AIR105_BSP__)
  7. #include "bsp_common.h"
  8. #endif
  9. #ifndef __BSP_COMMON_H__
  10. #include "c_common.h"
  11. #endif
  12. #define LUAT_LOG_TAG "mobile"
  13. #include "luat_log.h"
  14. typedef struct
  15. {
  16. llist_head node;
  17. PV_Union plmn;
  18. uint8_t ip_type;
  19. uint8_t protocol;
  20. uint8_t name_len;
  21. uint8_t user_len;
  22. uint8_t password_len;
  23. uint8_t unused[3];
  24. char data[0];
  25. }apn_node_t;
  26. typedef struct
  27. {
  28. llist_head dynamic_list;
  29. }auto_apn_ctrl_t;
  30. static auto_apn_ctrl_t luat_auto_apn;
  31. void luat_mobile_init_auto_apn_by_plmn(void)
  32. {
  33. luat_mobile_init_auto_apn();
  34. INIT_LLIST_HEAD(&luat_auto_apn.dynamic_list);
  35. }
  36. static int32_t luat_mobile_find_apn(void *node, void *param)
  37. {
  38. apn_node_t *apn = (apn_node_t *)node;
  39. if (apn->plmn.p == param)
  40. {
  41. return LIST_FIND;
  42. }
  43. return LIST_PASS;
  44. }
  45. void luat_mobile_add_auto_apn_item(uint16_t mcc, uint16_t mnc, uint8_t ip_type, uint8_t protocol, char *name, uint8_t name_len, char *user, uint8_t user_len, char *password, uint8_t password_len, uint8_t task_safe)
  46. {
  47. apn_node_t *apn;
  48. if (task_safe)
  49. {
  50. luat_rtos_task_suspend_all();
  51. }
  52. if (!luat_auto_apn.dynamic_list.next)
  53. {
  54. luat_mobile_init_auto_apn_by_plmn();
  55. }
  56. PV_Union plmn;
  57. plmn.u16[1] = mcc;
  58. plmn.u16[0] = mnc;
  59. apn = llist_traversal(&luat_auto_apn.dynamic_list, luat_mobile_find_apn, plmn.p);
  60. if (apn)
  61. {
  62. llist_del(&apn->node);
  63. free(apn);
  64. }
  65. apn = calloc(1, sizeof(apn_node_t) + name_len + user_len + password_len);
  66. apn->plmn.u16[1] = mcc;
  67. apn->plmn.u16[0] = mnc;
  68. apn->ip_type = ip_type;
  69. apn->protocol = protocol;
  70. apn->name_len = name_len;
  71. apn->user_len = user_len;
  72. apn->password_len = password_len;
  73. memcpy(apn->data, name, name_len);
  74. if (user && user_len)
  75. {
  76. memcpy(apn->data + name_len, user, user_len);
  77. }
  78. if (password && password_len)
  79. {
  80. memcpy(apn->data + name_len + user_len, password, password_len);
  81. }
  82. llist_add_tail(&apn->node, &luat_auto_apn.dynamic_list);
  83. if (task_safe)
  84. {
  85. luat_rtos_task_resume_all();
  86. }
  87. }
  88. int luat_mobile_find_apn_by_mcc_mnc(uint16_t mcc, uint16_t mnc, apn_info_t *info)
  89. {
  90. if (!luat_auto_apn.dynamic_list.next)
  91. {
  92. LLOGE("no apn table");
  93. return -1;
  94. }
  95. apn_node_t *apn;
  96. PV_Union plmn;
  97. plmn.u16[1] = mcc;
  98. plmn.u16[0] = mnc;
  99. int result;
  100. luat_rtos_task_suspend_all();
  101. apn = llist_traversal(&luat_auto_apn.dynamic_list, luat_mobile_find_apn, plmn.p);
  102. if (apn)
  103. {
  104. info->data = apn->data;
  105. info->ip_type = apn->ip_type;
  106. info->protocol = apn->protocol;
  107. info->name_len = apn->name_len;
  108. info->user_len = apn->user_len;
  109. info->password_len = apn->password_len;
  110. result = 0;
  111. }
  112. else
  113. {
  114. result = -1;
  115. }
  116. luat_rtos_task_resume_all();
  117. return result;
  118. }
  119. LUAT_WEAK int get_apn_info_by_static(uint16_t mcc, uint16_t mnc, apn_info_t *info)
  120. {
  121. return -1;
  122. }
  123. void luat_mobile_print_apn_by_mcc_mnc(uint16_t mcc, uint16_t mnc)
  124. {
  125. apn_info_t info;
  126. if(luat_mobile_find_apn_by_mcc_mnc(mcc, mnc, &info))
  127. {
  128. if (get_apn_info_by_static(mcc, mnc, &info))
  129. {
  130. LLOGD("mcc 0x%x, mnc 0x%x not find");
  131. return;
  132. }
  133. if (!info.data)
  134. {
  135. LLOGD("mcc 0x%x, mnc 0x%x no need apn");
  136. return;
  137. }
  138. }
  139. LLOGD("mcc 0x%x, mnc 0x%x, ip_type %d, auth_type %d, apn %dbyte %.*s, user %dbyte %.*s, password %dbyte %.*s",
  140. mcc, mnc, info.ip_type, info.protocol, info.name_len, info.name_len, info.data,
  141. info.user_len, info.user_len, info.user_len?(info.data + info.name_len):NULL,
  142. info.password_len, info.password_len, info.password_len?(info.data + info.name_len + info.user_len):NULL);
  143. }
  144. int luat_mobile_get_isp_from_plmn(uint16_t mcc, uint8_t mnc)
  145. {
  146. uint8_t cmcc[] = {0, 2, 4, 7, 8, 13, 23, 24};
  147. uint8_t cucc[] = {1, 6, 9, 10};
  148. uint8_t ctcc[] = {3, 5, 11, 12};
  149. if (mcc != 460) return -1;
  150. uint8_t i;
  151. for(i = 0; i < sizeof(cmcc); i++)
  152. {
  153. if (mnc == cmcc[i])
  154. {
  155. return LUAT_MOBILE_ISP_CMCC;
  156. }
  157. }
  158. for(i = 0; i < sizeof(ctcc); i++)
  159. {
  160. if (mnc == ctcc[i])
  161. {
  162. return LUAT_MOBILE_ISP_CTCC;
  163. }
  164. }
  165. for(i = 0; i < sizeof(cucc); i++)
  166. {
  167. if (mnc == cucc[i])
  168. {
  169. return LUAT_MOBILE_ISP_CUCC;
  170. }
  171. }
  172. if (mnc == 15) return LUAT_MOBILE_ISP_CRCC;
  173. return LUAT_MOBILE_ISP_UNKNOW;
  174. }
  175. int luat_mobile_get_plmn_from_imsi(char *imsi, uint16_t *mcc, uint8_t *mnc)
  176. {
  177. if (!imsi) return -1;
  178. uint16_t temp = imsi[0] - '0';
  179. temp = (temp * 10) + imsi[1] - '0';
  180. temp = (temp * 10) + imsi[2] - '0';
  181. *mcc = temp;
  182. temp = imsi[3] - '0';
  183. temp = (temp * 10) + imsi[4] - '0';
  184. *mnc = temp;
  185. return 0;
  186. }
  187. typedef struct{
  188. uint8_t band;
  189. uint32_t min_earfcn;
  190. uint32_t max_earfcn;
  191. }earfcn_to_band_t;
  192. static const earfcn_to_band_t g_band_list[] = {
  193. {1, 0, 599},
  194. {2, 600, 1199},
  195. {3, 1200, 1949},
  196. {4, 1950, 2399},
  197. {5, 2400, 2649},
  198. {7, 2750, 3449},
  199. {8, 3450, 3799},
  200. {9, 3800, 4149},
  201. {10, 4150, 4749},
  202. {11, 4750, 4949},
  203. {12, 5010, 5179},
  204. {13, 5180, 5279},
  205. {14, 5280, 5379},
  206. {17, 5730, 5849},
  207. {18, 5850, 5999},
  208. {19, 6000, 6149},
  209. {20, 6150, 6449},
  210. {21, 6450, 6599},
  211. {22, 6600, 7399},
  212. {24, 7700, 8039},
  213. {25, 8040, 8689},
  214. {26, 8690, 9039},
  215. {27, 9040, 9209},
  216. {28, 9210, 9659},
  217. {29, 9660, 9769},
  218. {30, 9770, 9869},
  219. {31, 9870, 9919},
  220. {32, 9920, 10359},
  221. {33, 36000, 36199},
  222. {34, 36200, 36349},
  223. {35, 36350, 36949},
  224. {36, 36950, 37549},
  225. {37, 37550, 37749},
  226. {38, 37750, 38249},
  227. {39, 38250, 38649},
  228. {40, 38650, 39649},
  229. {41, 39650, 41589},
  230. {42, 41590, 43589},
  231. {43, 43590, 45589},
  232. {44, 45590, 46589},
  233. {45, 46590, 46789},
  234. {46, 46790, 54539},
  235. {47, 54540, 55239},
  236. {48, 55240, 56739},
  237. {49, 56740, 58239},
  238. {50, 58240, 59089},
  239. {51, 59090, 59139},
  240. {52, 59140, 60139},
  241. {53, 60140, 60254},
  242. {65, 65536, 66435},
  243. {66, 66436, 67335},
  244. {67, 67336, 67535},
  245. {68, 67536, 67835},
  246. {69, 67836, 68335},
  247. {70, 68336, 68585},
  248. {71, 68586, 68935},
  249. {72, 68936, 68985},
  250. {73, 68986, 69035},
  251. {74, 69036, 69465},
  252. {75, 69466, 70315},
  253. {76, 70316, 70365},
  254. {85, 70366, 70545},
  255. {87, 70546, 70595},
  256. {88, 70596, 70645},
  257. };
  258. int luat_mobile_get_band_from_earfcn(uint32_t earfcn)
  259. {
  260. uint32_t l = 0;
  261. uint32_t h = (sizeof(g_band_list) / sizeof(g_band_list[0]) - 1);
  262. while (l <= h)
  263. {
  264. uint16_t index = l + ((h - l) >> 1);
  265. uint32_t min = g_band_list[index].min_earfcn;
  266. uint32_t max = g_band_list[index].max_earfcn;
  267. if (earfcn >= min && earfcn <= max)
  268. {
  269. return g_band_list[index].band;
  270. }
  271. else if(earfcn > max)
  272. {
  273. l = index + 1;
  274. }
  275. else if(earfcn < min)
  276. {
  277. h = index - 1;
  278. }
  279. }
  280. return 0;
  281. }