luat_mobile_common.c 6.6 KB

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