luat_mobile_common.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 int 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. }