Cellular.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8. #include <errno.h>
  9. #include "cJSON.h"
  10. #include "json_handle.h"
  11. #include "websocket_handle.h"
  12. #include "common.h"
  13. #include "sys.h"
  14. #include "Cellular.h"
  15. #include <pthread.h>
  16. #include "thread.h"
  17. #include "paras.h"
  18. #include "lock.h"
  19. #include "elog.h"
  20. #include "cfg.h"
  21. #if 0
  22. #define LOGE log_e
  23. #define LOGW log_w
  24. #define LOGD log_d
  25. #else
  26. #define LOGD printf
  27. #define LOGW printf
  28. #define LOGE printf
  29. #endif
  30. //#define USE_CELL
  31. //#define GNSS_ENABLE //EC200N-CN, 不支持gnss
  32. #define RETRY_TIMES 3
  33. #define BUF_LEN 2000
  34. #define AT_PORT "/dev/ttyUSB1"
  35. #define BAUD_RATE B115200
  36. #define QUERY_INTERVAL 1000 // 查询间隔,毫秒
  37. #define RSSI_DBM_MIN1 -113 // LTE、HSDPA网络最小信号值,对应RSSI值范围0-57
  38. #define RSSI_DBM_MIN2 -116 // TD-SCDMA网络最小信号值,对应RSSI值范围100-199
  39. #define CONTEXT_ID "1" //1~15
  40. #define CONNECT_ID "0" //0~11
  41. #define CONNECT_TIMEOUT "5"
  42. #define CHUNCK_SIZE 1460
  43. #define MQTT_VER 4 //3: v3.1 4: v3.1.1
  44. #define MQTT_WILL 0
  45. #define MQTT_RETAIN 0
  46. #define MQTT_MAX 6
  47. #define MQTT_QOS 2
  48. #define MQTT_CHUNCK_SIZE 1500
  49. enum {
  50. AT_OK=0,
  51. AT_ERR,
  52. AT_TOUT,
  53. };
  54. enum {
  55. AT_STAT_UNINIT=0,
  56. AT_STAT_INITED,
  57. };
  58. enum {
  59. SOCK_TCP=0,
  60. SOCK_UDP,
  61. };
  62. enum {
  63. URC_SOCK=0,
  64. URC_MQTT,
  65. URC_GNSS,
  66. URC_NTP,
  67. URC_SMS,
  68. URC_CALL,
  69. URC_ERR,
  70. };
  71. typedef int (*urc_fn_t)(int urc, void *data, int len);
  72. typedef struct {
  73. int cmd;
  74. char para[100];
  75. }at_para_t;
  76. typedef struct {
  77. char *cmd;
  78. char *ok;
  79. char *err;
  80. int timeout; //ms
  81. }at_cmd_t;
  82. typedef struct {
  83. int cmd;
  84. char *buf;
  85. int dlen;
  86. int blen;
  87. }at_buf_t;
  88. typedef struct {
  89. at_buf_t rx;
  90. at_buf_t tx;
  91. }at_data_t;
  92. typedef struct {
  93. void *h;
  94. int sock;
  95. int cont_id; //1~15
  96. int cont_type; //1:ipv4 2:ipv6 3:ipv4+ipv6
  97. int conn_id;
  98. char apn[64];
  99. char username[128];
  100. char password[128];
  101. int authentication; //0:none 1:PAP 2:CHAP 3:PAP OR CHAP
  102. int cdma_pwd; //0:no save 1:save
  103. }sock_conn_t;
  104. typedef struct {
  105. char host[100];
  106. uint16_t port;
  107. char username[64];
  108. char password[64];
  109. }mqtt_para_t;
  110. typedef struct {
  111. int flag;
  112. int qos;
  113. int retain;
  114. char topic[256];
  115. char message[256];
  116. int message_len;
  117. }mqtt_will_t;
  118. typedef struct {
  119. char product_key[64]; //aliyun used
  120. char device_name[64]; //aliyun used
  121. char device_secret[64]; //aliyun/ used
  122. char product_id[64]; //onenet used
  123. char access_key[64]; //onenet used
  124. int hw_time_enable; //huawei used
  125. char product_secret[64]; //huawei used
  126. char node_id[64];
  127. }mqtt_plat_t;
  128. typedef struct {
  129. void *h;
  130. int ver; //3:v3.1 4:v3.1.1
  131. int pdpId; //1~15
  132. int cliId;
  133. uint16_t msgId;
  134. int qos;
  135. int retain;
  136. mqtt_will_t will;
  137. int timeout; //1~60, default:5
  138. int retry_times;
  139. int timeout_notice;
  140. int clean_session;
  141. int keep_alive_time; //0~3600 seconds default:120, 0:do not disconnect
  142. int ssl_enable;
  143. int ssl_ctx_id;
  144. int msg_recv_mode; //0: urc, 1: not urc
  145. int msg_len_enable;
  146. int ping_interval; //5~60, default:5
  147. int proto_chk_enable; //0:no check 1:check
  148. int send_mode; //0:string 1:hex
  149. int recv_mode; //0:string 1:hex
  150. int view_mode; //0:no echo 1:echo
  151. int edit_mode; //0:timeout not quit 1:timeout quit
  152. int edit_time; //1~120 seconds
  153. mqtt_plat_t plat;
  154. }mqtt_conn_t;
  155. typedef struct {
  156. int fd;
  157. CellInfo_t *pcell;
  158. lock_t lck;
  159. int prod_id;
  160. int stat;
  161. cell_all_t all;
  162. int curcmd; //current cmd
  163. at_data_t data[AT_CMD_MAX];
  164. GlobalDeviceManager *dm;
  165. }cell_handle_t;
  166. static cell_handle_t ceHandle={0};
  167. static at_cmd_t all_cmd[AT_CMD_MAX]={
  168. //genernal cmd
  169. {"AT", "OK", "ERROR", 300}, //测试命令
  170. {"ATI", "OK", "ERROR", 300}, //显示MT的ID信息
  171. {"AT+GMI", "OK", "ERROR", 300}, //请求制造商信息
  172. {"AT+GMM", "OK", "ERROR", 300}, //请求TA型号ID
  173. {"AT+GMR", "OK", "ERROR", 300}, //请求TA固件版本ID
  174. {"AT+CGMI", "OK", "ERROR", 300}, //同GMI
  175. {"AT+CGMM", "OK", "ERROR", 300}, //同GMM
  176. {"AT+CGMR", "OK", "ERROR", 300}, //同GMR
  177. {"AT+GSN", "OK", "ERROR", 300}, //请求国际移动设备识别码(IMEI)
  178. {"AT+CGSN", "OK", "ERROR", 300}, //同GSN
  179. {"AT&F", "OK", "ERROR", 300}, //重置AT命令设置为出厂设置
  180. {"AT&V", "OK", "ERROR", 300}, //显示当前配置
  181. {"AT&W", "OK", "ERROR", 300}, //存储当前设置到用户自定义配置文件
  182. {"ATZ", "OK", "ERROR", 300}, //从用户定义配置文件还原所有AT命令设置
  183. {"ATQ", "OK", "ERROR", 300}, //设置结果码回显模式
  184. {"ATV", "OK", "ERROR", 300}, //TA响应格式
  185. {"ATE0", "OK", "ERROR", 300}, //设置命令回显模式
  186. {"ATS3", "OK", "ERROR", 300}, //设置命令行终止符
  187. {"ATS4", "OK", "ERROR", 300}, //设置响应格式字符
  188. {"ATS5", "OK", "ERROR", 300}, //设置命令行编辑字符
  189. {"ATX", "OK", "ERROR", 300}, //设置CONNECT结果码格式和检测呼叫进程
  190. {"AT+CFUN", "OK", "ERROR", 15000}, //设置功能模式
  191. {"AT+CMEE", "OK", "ERROR", 300}, //设置错误讯息格式
  192. {"AT+CSCS", "OK", "ERROR", 300}, //选择TE字符集
  193. {"AT+QURCCFG", "OK", "ERROR", 300}, //配置URC指示选项
  194. {"AT+QPPPDROP", "OK", "ERROR", 300}, //终止PPP连接
  195. //serial cmd
  196. {"AT&C", "OK", "ERROR", 300}, //设置DCD信号模式
  197. {"AT&D", "OK", "ERROR", 300}, //设置DTR信号模式
  198. {"AT+IFC", "OK", "ERROR", 300}, //设置串口的流控方式
  199. {"AT+IPR", "OK", "ERROR", 300}, //设置固定的串口通信波特率
  200. //status cmd
  201. {"AT+CPAS", "OK", "ERROR", 300}, //查询ME活动状态
  202. {"AT+CEER", "OK", "ERROR", 300}, //上报扩展错误
  203. {"AT+QCFG", "OK", "ERROR", 300}, //扩展命令, 较多子命令
  204. {"AT+QINDCFG", "OK", "ERROR", 300}, //控制URC上报
  205. //sim cmd
  206. {"AT+CIMI", "OK", "ERROR", 300}, //查询IMSI
  207. {"AT+CLCK", "OK", "ERROR", 5000}, //功能锁定
  208. {"AT+CPIN", "OK", "ERROR", 5000}, //PIN管理
  209. {"AT+CPWD", "OK", "ERROR", 5000}, //修改密码
  210. {"AT+CSIM", "OK", "ERROR", 300}, //通用(U)SIM卡访问
  211. {"AT+CRSM", "OK", "ERROR", 300}, //(U)SIM卡受限访问
  212. {"AT+QCCID", "OK", "ERROR", 300}, //查询ICCID
  213. {"AT+QPINC", "OK", "ERROR", 300}, //获取PIN剩余重试次数
  214. {"AT+QINISTAT", "OK", "ERROR", 300}, //查询(U)SIM卡初始化状态
  215. {"AT+QSIMDET", "OK", "ERROR", 300}, //(U)SIM卡检测
  216. {"AT+QSIMSTAT", "OK", "ERROR", 300}, //(U)SIM卡插拔状态上报
  217. {"AT+QDSIM", "OK", "ERROR", 50000}, //选择(U)SIM卡
  218. //network service cmd
  219. {"AT+COPS", "OK", "ERROR", 300000}, //选择运营商
  220. {"AT+CREG", "OK", "ERROR", 300}, //CS域网络注册状态
  221. {"AT+CSQ", "OK", "ERROR", 300}, //查询信号强度
  222. {"AT+CPOL", "OK", "ERROR", 300}, //配置首选运营商列表
  223. {"AT+COPN", "OK", "ERROR", 300}, //查询运营商名称列表
  224. {"AT+CTZU", "OK", "ERROR", 300}, //自动更新时区
  225. {"AT+CTZR", "OK", "ERROR", 300}, //上报时区变化
  226. {"AT+QLTS", "OK", "ERROR", 500}, //获取通过网络同步的最新时间
  227. {"AT+QNWINFO", "OK", "ERROR", 300}, //查询网络信息
  228. //call cmd
  229. {"ATA", "OK", "ERROR", 90000}, //呼叫应答
  230. {"ATD", "OK", "ERROR", 5000}, //发起呼叫
  231. {"AT+COLP", "OK", "ERROR", 15000}, //被叫线路识别显示
  232. {"AT+ATH", "OK", "ERROR", 90000}, //挂断现有连接
  233. {"AT+CHUP", "OK", "ERROR", 90000}, //挂断语音通话
  234. {"+++", "OK", "ERROR", 300}, //从数据模式切换至命令模式
  235. {"AT+ATO", "OK", "ERROR", 300}, //从命令模式切换至数据模式
  236. {"AT+ATSO", "OK", "ERROR", 300}, //设置自动应答前振铃次数
  237. {"AT+CLCC", "OK", "ERROR", 300}, //查询当前 ME 呼叫
  238. {"AT^DSCI", "OK", "ERROR", 300}, //指示呼叫状态
  239. //phonebook cmd
  240. {"AT+CNUM", "OK", "ERROR", 300}, //查询本机号码
  241. {"AT+CPBF", "OK", "ERROR", 300}, //搜索电话本
  242. {"AT+CPBR", "OK", "ERROR", 300}, //读取电话本条目
  243. {"AT+CPBS", "OK", "ERROR", 300}, //设置电话本存储器
  244. {"AT+CPBW", "OK", "ERROR", 300}, //写入电话本
  245. //sms cmd
  246. {"AT+CSMS", "OK", "ERROR", 300}, //选择短消息服务类型
  247. {"AT+CMGF", "OK", "ERROR", 300}, //配置短消息模式
  248. {"AT+CSCA", "OK", "ERROR", 300}, //设置短消息服务中心地址
  249. {"AT+CPMS", "OK", "ERROR", 300}, //配置短消息首选存储位置
  250. {"AT+CMGD", "OK", "ERROR", 300}, //删除短消息
  251. {"AT+CMGL", "OK", "ERROR", 300}, //按状态读取短消息
  252. {"AT+CMGR", "OK", "ERROR", 300}, //按照索引读取短消息
  253. {"AT+CMGS", ">", "ERROR", 120000}, //发送短消息
  254. {"AT+CMMS", "OK", "ERROR", 300}, //发送多条短消息
  255. {"AT+CMGW", "OK", "ERROR", 300}, //存储短消息
  256. {"AT+CMSS", "OK", "ERROR", 300}, //从存储器发送短消息
  257. {"AT+CNMA", "OK", "ERROR", 300}, //新短消息确认
  258. {"AT+CNMI", "OK", "ERROR", 300}, //设置短消息上报方式
  259. {"AT+CSCB", "OK", "ERROR", 300}, //选择小区广播消息类型
  260. {"AT+CSDH", "OK", "ERROR", 300}, //设置文本模式下参数显示
  261. {"AT+CSMP", "OK", "ERROR", 300}, //设置短消息文本模式参数
  262. {"AT+QCMGS", "OK", "ERROR", 120000}, //发送级联短消息
  263. {"AT+QCMGR", "OK", "ERROR", 300}, //读取级联短消息
  264. //gps cmd
  265. {"AT+QGPSCFG", "OK", "ERROR", 300}, //配置GNSS
  266. {"AT+QGPSDEL", "OK", "ERROR", 300}, //删除辅助数据
  267. {"AT+QGPS", "OK", "ERROR", 300}, //打开GNSS
  268. {"AT+QGPSEND", "OK", "ERROR", 300}, //关闭GNSS
  269. {"AT+QGPSLOC", "OK", "ERROR", 300}, //获取定位信息
  270. {"AT+QGPSNMEA", "OK", "ERROR", 300}, //获取指定的NMEA语句
  271. {"AT+QAGPS", "OK", "ERROR", 300}, //启用/禁用AGPS
  272. //tcp/udp cmd
  273. {"AT+QICFG", "OK", "ERROR", 300}, //配置可选参数
  274. {"AT+QICSGP", "OK", "ERROR", 300}, //配置TCP/IP场景参数
  275. {"AT+QIACT", "OK", "ERROR", 10000}, //激活PDP场景
  276. {"AT+QIDEACT", "OK", "ERROR", 10000}, //去激活PDP场景
  277. {"AT+QIOPEN", "OK", "ERROR", 300}, //打开Socket服务
  278. {"AT+QICLOSE", "OK", "ERROR", 300}, //关闭Socket服务
  279. {"AT+QISTATE", "OK", "ERROR", 300}, //查询Socket服务状态
  280. {"AT+QISEND", ">", "ERROR", 300}, //发送数据
  281. {"AT+QIRD", "OK", "ERROR", 300}, //读取收到的TCP/IP数据
  282. {"AT+QISENDEX", "OK", "ERROR", 300}, //发送16进制字符串数据
  283. {"AT+QISWTMD", "OK", "ERROR", 300}, //切换数据访问模式
  284. {"AT+QNTP", "OK", "ERROR", 300}, //进行远程服务器Ping操作
  285. {"AT+QPING", "+QPING", "ERROR", 3000}, //使用NTP服务器同步本地时间
  286. {"AT+QIDNSCFG", "OK", "ERROR", 300}, //配置DNS服务器地址
  287. {"AT+QIDNSGIP", "OK", "ERROR", 300}, //用域名获取IP地址
  288. {"AT+QISDE", "OK", "ERROR", 300}, //控制是否回显AT+QISEND要发送的
  289. {"AT+QIGETERROR", "OK", "ERROR", 300}, //查询上一个AT命令错误代码
  290. //mqtt cmd
  291. {"AT+QMTCFG", "OK", "ERROR", 300}, //配置MQTT可选参数
  292. {"AT+QMTOPEN", "OK", "ERROR", 300}, //打开MQTT客户端网络
  293. {"AT+QMTCLOSE", "OK", "ERROR", 300}, //关闭MQTT客户端网络
  294. {"AT+QMTCONN", "OK", "ERROR", 300}, //连接客户端到MQTT服务器
  295. {"AT+QMTDISC", "OK", "ERROR", 300}, //客户端断开与MQTT服务器的连接
  296. {"AT+QMTSUB", "OK", "ERROR", 300}, //订阅主题
  297. {"AT+QMTUNS", "OK", "ERROR", 300}, //退订主题
  298. {"AT+QMTPUBEX", ">", "ERROR", 300}, //发布消息
  299. {"AT+QMTRECV", "OK", "ERROR", 300}, //从缓存中读取消息
  300. //domain cmd
  301. {"AT+CGATT", "OK", "ERROR", 140000}, //PS域附着或去附着
  302. {"AT+CGDCONT", "OK", "ERROR", 3000}, //定义PDP上下文
  303. {"AT+CGQREQ", "OK", "ERROR", 300}, //服务质量参数请求
  304. {"AT+CGQMIN", "OK", "ERROR", 300}, //服务质量参数(最低可接受的)
  305. {"AT+CGEQREQ", "OK", "ERROR", 300}, //3G服务质量参数(请求的)
  306. {"AT+CGEQMIN", "OK", "ERROR", 300}, //3G 服务质量参数(最低可接受的)
  307. {"AT+CGACT", "OK", "ERROR", 150000}, //PDP上下文激活/去激活
  308. {"AT+CGDATA", "OK", "ERROR", 300}, //进入数据传输状态
  309. {"AT+CGPADDR", "OK", "ERROR", 300}, //显示PDP地址
  310. {"AT+CGCLASS", "OK", "ERROR", 300}, //GPRS移动站点等级
  311. {"AT+CGEREG", "OK", "ERROR", 300}, //PS域网络注册状态
  312. {"AT+CGEREP", "OK", "ERROR", 300}, //分组域事件上报
  313. {"AT+CGSMS", "OK", "ERROR", 300}, //选择MO短消息服务
  314. {"AT+CEREG", "OK", "ERROR", 300}, //EPS网络注册状态
  315. {"AT+QGDCNT", "OK", "ERROR", 300}, //流量统计
  316. {"AT+QAUGDCNT", "OK", "ERROR", 300}, //自动保存流量统计
  317. {"AT+QNETDEVCTL", "OK", "ERROR", 2000}, //连接USB网卡
  318. {"AT+QCEERCATCFG", "OK", "ERROR", 300}, //配置2G/4G下AT+CEER返回格式
  319. //hardware cmd
  320. {"AT+QPOWD", "OK", "ERROR", 300}, //关闭模块
  321. {"AT+CCLK", "OK", "ERROR", 300}, //实时时钟
  322. {"AT+QSCLK", "OK", "ERROR", 300}, //禁用/启用睡眠模式
  323. };
  324. at_para_t init_para[]={
  325. {AT_CMD_AT, ""},
  326. {AT_CMD_ATE, "0"},
  327. {AT_CMD_CPIN, "?"},
  328. {AT_CMD_CREG, "?"},
  329. //{AT_CMD_QIDEACT, "="CONTEXT_ID},
  330. {AT_CMD_QIACT, "="CONTEXT_ID},
  331. {AT_CMD_CGDCONT, "="CONTEXT_ID",\"IPV4V6\",\"cmnet\""},
  332. {AT_CMD_QCFG, "=\"usbnet\",3"}, //set module to rndis mode
  333. {AT_CMD_CMGF, "=1"},
  334. {AT_CMD_CSCS, "=GSM"},
  335. #ifdef GNSS_ENABLE
  336. {AT_CMD_QGPS, "=1"},
  337. {AT_CMD_QGPSCFG, "=\"gnssconfig\",5"},
  338. {AT_CMD_QGPSCFG, "=\"gpsnmeatype\",1"},
  339. #endif
  340. {-1, ""},
  341. };
  342. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  343. static at_cmd_t* data_of(int cmd)
  344. {
  345. return &all_cmd[cmd];
  346. }
  347. static int rssi_to_dbm(int rssi)
  348. {
  349. int dbm=0;
  350. if(rssi>=0 && rssi<=31) {
  351. dbm = RSSI_DBM_MIN1+rssi*2;
  352. }
  353. else if(rssi>=100 && rssi<=191) {
  354. dbm = RSSI_DBM_MIN2+rssi;
  355. }
  356. return dbm;
  357. }
  358. static char* str_get(char *buf, char c)
  359. {
  360. int i,len=strlen(buf);
  361. for(i=0; i<len; i++) {
  362. if(buf[i]==c) return buf+i;
  363. }
  364. return buf;
  365. }
  366. static int mem_find(char *buf, int len, char *str)
  367. {
  368. int i,slen=strlen(str);
  369. for(i=0; i<=len-slen; i++) {
  370. if(memcmp(buf+i, str, slen)==0) {
  371. return 0;
  372. }
  373. }
  374. return -1;
  375. }
  376. static void mem_print(char *buf, int len)
  377. {
  378. int i;
  379. for(i=0; i<len; i++) {
  380. LOGD("%d, ", buf[i]);
  381. if((i+1)%16==0) LOGD("\n");
  382. }
  383. LOGD("\n");
  384. }
  385. static int cell_reset(cell_handle_t *h);
  386. static int cell_sock_init(cell_handle_t *h);
  387. static int cell_get_info(cell_handle_t *h, cell_info_t *info);
  388. static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat);
  389. //////////////////////////////////////////////////////////////////////////////////
  390. #ifdef USE_CELL
  391. static int port_setup(cell_handle_t *h, int speed);
  392. static void msleep(int ms)
  393. {
  394. usleep(ms*1000);
  395. }
  396. static void reset_var(CellularManager *pcell)
  397. {
  398. //pcell->mode = 0;
  399. pcell->Link_status = false; // 4G网络连接状态
  400. pcell->Signal_strength = 0; // 4G信号强度
  401. sprintf(pcell->Cellular_name, "-");
  402. sprintf(pcell->Cellular_Num, "-");
  403. //pcell->Cellular_IMEI[64]="-"; // IMEI号
  404. // pcell->GPS_status = false; // GPS功能启用
  405. pcell->GPS_latitude = 0.0; // GPS��度
  406. pcell->GPS_longitude = 0.0; // GPS经度
  407. pcell->GPS_altitude = 0.0; // GPS海��高度
  408. pcell->GPS_speed = 0.0; // GPS速度,单位km/h
  409. pcell->GPS_nsat = 0; // GPS��星数量
  410. sprintf(pcell->GPS_utc, "-");
  411. //pcell->SMS_status = false; // 短信功能启用
  412. }
  413. static int port_init(cell_handle_t *h)
  414. {
  415. h->fd = open(AT_PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);
  416. if(h->fd<0) {
  417. return -1;
  418. }
  419. port_setup(h, BAUD_RATE);
  420. //reset_var(h->pcell);
  421. return 0;
  422. }
  423. static int port_deinit(cell_handle_t *h)
  424. {
  425. if(h->fd<0) {
  426. return -1;
  427. }
  428. return close(h->fd);
  429. }
  430. static int port_read(cell_handle_t *h, void *data, int len)
  431. {
  432. return read(h->fd, data, len);
  433. }
  434. static int port_write(cell_handle_t *h, void *data, int len)
  435. {
  436. return write(h->fd, data, len);
  437. }
  438. static int port_setup(cell_handle_t *h, int speed)
  439. {
  440. struct termios tty;
  441. if (tcgetattr(h->fd, &tty) != 0) {
  442. LOGE("Read 4G Module tcgetattr!!!\n");
  443. return -1;
  444. }
  445. cfsetospeed(&tty, speed);
  446. cfsetispeed(&tty, speed);
  447. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
  448. tty.c_iflag &= ~IGNBRK; // disable break processing
  449. tty.c_lflag = 0; // no signaling chars, no echo,
  450. // no canonical processing
  451. tty.c_oflag = 0; // no remapping, no delays
  452. tty.c_cc[VMIN] = 0; // read doesn't block
  453. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  454. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  455. tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
  456. // enable reading
  457. tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
  458. tty.c_cflag |= 0;
  459. tty.c_cflag &= ~CSTOPB;
  460. tty.c_cflag &= ~CRTSCTS;
  461. if (tcsetattr(h->fd, TCSANOW, &tty) != 0) {
  462. LOGE("Read 4G Module tcsetattr!!!\n");
  463. return -1;
  464. }
  465. return 0;
  466. }
  467. static int port_wait(cell_handle_t *h, int timeout)
  468. {
  469. int r;
  470. char buf[100];
  471. fd_set fset;
  472. struct timeval tv;
  473. tv.tv_sec = timeout/1000;
  474. tv.tv_usec = (timeout%1000)*1000;
  475. FD_ZERO(&fset); FD_SET(h->fd, &fset);
  476. r = select(h->fd + 1, &fset, NULL, NULL, &tv);
  477. if (r<=0) {
  478. if(r<0) {
  479. //LOGE("port_wait error %s\n", strerror(errno));
  480. }
  481. else {
  482. //LOGE("port_wait timeout\n");
  483. }
  484. }
  485. return r;
  486. }
  487. static int port_recv(cell_handle_t *h, char *buf, int buflen, int timeout)
  488. {
  489. int r;
  490. r = port_wait(h, timeout);
  491. if(r<0) {
  492. return -1;
  493. }
  494. r = port_read(h, buf, buflen);
  495. if(r<=0) {
  496. return -1;
  497. }
  498. return r;
  499. }
  500. static int send_cmd(cell_handle_t *h, int cmd, char *para, char *rcv, int rcvlen)
  501. {
  502. int i,r,l,tout,finish=0;
  503. int tlen=0,buflen,wait_step=10;
  504. char buf[BUF_LEN],*pbuf;
  505. at_cmd_t *pcmd=data_of(cmd);
  506. sprintf(buf, "%s%s\r", pcmd->cmd, para?para:"");
  507. LOGD("[tx]: %s\n", buf);
  508. for(i=0; i<RETRY_TIMES; i++) {
  509. tout = 0;
  510. port_write(h, buf, strlen(buf));
  511. while(tout<pcmd->timeout) {
  512. l = port_wait(h, wait_step);
  513. if(l>0) {
  514. if(rcv && rcvlen>0) {
  515. pbuf = rcv;
  516. buflen = rcvlen;
  517. }
  518. else {
  519. pbuf = buf;
  520. buflen = sizeof(buf);
  521. }
  522. l = port_read(h, pbuf+tlen, buflen);
  523. if(l>0) {
  524. tlen += l; pbuf[tlen] = 0;
  525. LOGD("[rx]: %s\n", pbuf);
  526. if(pcmd->ok && strlen(pcmd->ok) && strstr(pbuf, pcmd->ok)) {
  527. return AT_OK;
  528. }
  529. if(pcmd->err && strlen(pcmd->err) && strstr(pbuf, pcmd->err)) {
  530. return AT_ERR;
  531. }
  532. }
  533. }
  534. tout += wait_step;
  535. }
  536. }
  537. cell_reset(h);
  538. return AT_TOUT;
  539. }
  540. static int send_data(cell_handle_t *h, void *data, int len, int timeout)
  541. {
  542. char buf[100];
  543. int l,tlen=0,tout=0,wait_step=10;
  544. l = port_write(h, data, len);
  545. if(l==len) {
  546. while(tout<timeout) {
  547. l = port_wait(h, wait_step);
  548. if(l>0) {
  549. l = port_read(h, buf, sizeof(buf));
  550. if(l>0) {
  551. tlen += l; buf[tlen] = 0;
  552. if(strstr(buf, "OK")) {
  553. return AT_OK;
  554. }
  555. else if(strstr(buf, "ERROR")) {
  556. return AT_ERR;
  557. }
  558. }
  559. }
  560. tout += wait_step;
  561. }
  562. }
  563. return AT_TOUT;
  564. }
  565. ////////////////////////////////////////////////////////////////////////////
  566. static int cell_reset(cell_handle_t *h)
  567. {
  568. int r;
  569. r = send_cmd(h, AT_CMD_CFUN, "=1,1", NULL, 0);
  570. if(r==AT_OK) {
  571. h->stat = AT_STAT_UNINIT;
  572. }
  573. return (r==AT_OK)?0:-1;
  574. }
  575. static int cell_init(cell_handle_t *h)
  576. {
  577. int i,r;
  578. at_para_t *p=init_para;
  579. if(h->stat==AT_STAT_INITED) {
  580. return 0;
  581. }
  582. for(i=0;; i++) {
  583. if(p[i].cmd==-1) {
  584. break;
  585. }
  586. if(p[i].cmd==AT_CMD_CGDCONT) {
  587. sprintf(p[i].para, "=%s,\"IPV4V6\",\"%s\"", CONTEXT_ID, h->pcell->apn);
  588. }
  589. else if(p[i].cmd==AT_CMD_QICSGP) {
  590. sprintf(p[i].para, "=%s,3,\"%s\"", CONTEXT_ID, h->pcell->apn);
  591. }
  592. r = send_cmd(h, p[i].cmd, p[i].para, NULL, 0);
  593. if(r!=AT_OK) {
  594. break;
  595. }
  596. }
  597. if(r==0) {
  598. h->stat = AT_STAT_INITED;
  599. cell_get_info(h, &h->all.info);
  600. }
  601. return r?-1:0;
  602. }
  603. static int cell_deinit(cell_handle_t *h)
  604. {
  605. if(h->stat==AT_STAT_UNINIT) {
  606. return -1;
  607. }
  608. return 0;
  609. }
  610. static int cell_get_info(cell_handle_t *h, cell_info_t *info)
  611. {
  612. int r;
  613. char buf[400];
  614. if(h->stat==AT_STAT_UNINIT) {
  615. return -1;
  616. }
  617. memset(info, 0, sizeof(cell_info_t));
  618. r = send_cmd(h, AT_CMD_CNUM, NULL, buf, sizeof(buf));
  619. if(r==AT_OK) {
  620. r = sscanf(str_get(buf,'+'), "+CNUM: %*s,%s,%*d", info->cnum);
  621. }
  622. r = send_cmd(h, AT_CMD_GSN, NULL, buf, sizeof(buf));
  623. if(r==AT_OK) {
  624. r = sscanf(str_get(buf,'+'), "%s", info->imei);
  625. }
  626. r = send_cmd(h, AT_CMD_CIMI, NULL, buf, sizeof(buf));
  627. if(r==AT_OK) {
  628. r = sscanf(str_get(buf,'+'), "%s", info->imsi);
  629. }
  630. r = send_cmd(h, AT_CMD_CGDCONT, "?", buf, sizeof(buf));
  631. if(r==AT_OK) {
  632. r = sscanf(str_get(buf,'+'), "+CGDCONT: %*d,\"%*[^\"]\",\"%[^\"]", info->apn);
  633. }
  634. r = send_cmd(h, AT_CMD_COPS, "?", buf, sizeof(buf));
  635. if(r==AT_OK) {
  636. r = sscanf(str_get(buf,'+'), "+COPS: %*d,%*d,\"%[^\"]", info->isp);
  637. }
  638. LOGD("___info->apn: %s\n", info->apn);
  639. LOGD("___info->isp: %s\n", info->isp);
  640. LOGD("___info->imei: %s\n", info->imei);
  641. LOGD("___info->imsi: %s\n", info->imsi);
  642. LOGD("___info->cnum: %s\n", info->cnum);
  643. return 0;
  644. }
  645. static int cell_get_sig(cell_handle_t *h, cell_stat_t *stat)
  646. {
  647. char buf[200];
  648. int rssi,ber,r=-1;
  649. if(h->stat==AT_STAT_UNINIT) {
  650. return -1;
  651. }
  652. r = send_cmd(h, AT_CMD_CSQ, NULL, buf, sizeof(buf));
  653. if(r!=AT_OK) {
  654. return -1;
  655. }
  656. r = sscanf(str_get(buf,'+'), "%*s %d,%d", &rssi, &ber);
  657. if(r<2) {
  658. return -1;
  659. }
  660. stat->dbm = rssi_to_dbm(rssi);
  661. stat->ber = ber;
  662. return 0;
  663. }
  664. static int cell_get_gnss(cell_handle_t *h, cell_stat_t *stat)
  665. {
  666. int r;
  667. char buf[200],utc[32],date[32];
  668. #ifdef GNSS_ENABLE
  669. if(h->stat==AT_STAT_UNINIT) {
  670. return -1;
  671. }
  672. r = send_cmd(h, AT_CMD_QGPSLOC, "=0", buf, sizeof(buf));
  673. if(r!=AT_OK) {
  674. return -1;
  675. }
  676. r = sscanf(str_get(buf,'+'), "+QGPSLOC: %s,%s,%s,%*d,%d,%*d,%*d,%*d,%*d,%s,%d", utc, stat->latitude, stat->longitude, &stat->altitude, date, &stat->nsta);
  677. if(r<6) {
  678. return -1;
  679. }
  680. //
  681. #endif
  682. return 0;
  683. }
  684. static int cell_get_ping(cell_handle_t *h, cell_stat_t *stat)
  685. {
  686. int r,ret;
  687. char buf[200],utc[32],date[32];
  688. if(h->stat==AT_STAT_UNINIT) {
  689. return -1;
  690. }
  691. r = send_cmd(h, AT_CMD_QPING, "=1,\"163.com\",3,2,", buf, sizeof(buf));
  692. if(r!=AT_OK) {
  693. return -1;
  694. }
  695. r = sscanf(str_get(buf,'+'), "+QPING: %d", &ret);
  696. if(r<1) {
  697. return -1;
  698. }
  699. if(ret==0) {
  700. stat->linked = 1;
  701. }
  702. else {
  703. stat->linked = 0;
  704. }
  705. return 0;
  706. }
  707. static int cell_get_time(cell_handle_t *h, cell_stat_t *stat)
  708. {
  709. int r,offset;
  710. char opr,buf[200],date[24],time[24];
  711. struct tm utc_tm;
  712. if(h->stat==AT_STAT_UNINIT) {
  713. return -1;
  714. }
  715. r = send_cmd(h, AT_CMD_QLTS, "=1", buf, sizeof(buf));
  716. if(r!=AT_OK) {
  717. return -1;
  718. }
  719. r = sscanf(str_get(buf,'+'), "+QLTS: \"%d/%d/%d,%d:%d:%d",&utc_tm.tm_year,&utc_tm.tm_mon,&utc_tm.tm_mday,&utc_tm.tm_hour,&utc_tm.tm_min,&utc_tm.tm_sec);
  720. if(r<6) {
  721. return -1;
  722. }
  723. stat->utctime = mktime2(&utc_tm, 0);
  724. //print_time("999", &utc_tm);
  725. return 0;
  726. }
  727. static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat)
  728. {
  729. if(h->stat==AT_STAT_UNINIT) {
  730. return -1;
  731. }
  732. cell_get_sig(h, stat);
  733. cell_get_gnss(h, stat);
  734. cell_get_ping(h, stat);
  735. cell_get_time(h, stat);
  736. return 0;
  737. }
  738. static int cell_sock_conn(cell_handle_t *h, int sock, char *host, uint16_t port, sock_conn_t *conn)
  739. {
  740. int r;
  741. char buf[200];
  742. if(h->stat==AT_STAT_UNINIT) {
  743. return -1;
  744. }
  745. sprintf(buf, "=%s,%s,%s,“%s”,%d,0,1", CONTEXT_ID, CONNECT_ID, (sock==SOCK_TCP)?"TCP":"UDP", host, port);
  746. r = send_cmd(h, AT_CMD_QIOPEN, buf, NULL, 0);
  747. if(r!=AT_OK) {
  748. return -1;
  749. }
  750. conn->h = h;
  751. conn->sock = sock;
  752. conn->cont_id = atoi(CONTEXT_ID);
  753. conn->conn_id = atoi(CONNECT_ID);
  754. return 0;
  755. }
  756. static int cell_sock_disconn(sock_conn_t *conn)
  757. {
  758. int r;
  759. char buf[200];
  760. cell_handle_t *h=(cell_handle_t*)conn->h;
  761. if(h->stat==AT_STAT_UNINIT) {
  762. return -1;
  763. }
  764. sprintf(buf, "=%s,%s", CONNECT_ID, CONNECT_TIMEOUT);
  765. r = send_cmd(h, AT_CMD_QICLOSE, buf, NULL, 0);
  766. if(r!=AT_OK) {
  767. return -1;
  768. }
  769. conn->h = NULL;
  770. conn->sock = -1;
  771. conn->cont_id = -1;
  772. conn->conn_id = -1;
  773. return 0;
  774. }
  775. static int cell_sock_send(sock_conn_t *conn, void *data, int len)
  776. {
  777. int r=0,rt,offset=0,remain_len,send_len;
  778. char buf[100],*pd=(char*)data;
  779. cell_handle_t *h=(cell_handle_t*)conn->h;
  780. if(h->stat==AT_STAT_UNINIT) {
  781. return -1;
  782. }
  783. sprintf(buf, "=%d,%d", conn->conn_id, len);
  784. rt = send_cmd(h, AT_CMD_QISEND, buf, NULL, 0);
  785. if(rt!=AT_OK) {
  786. return -1;
  787. }
  788. while(offset<len) {
  789. remain_len = len - offset;
  790. send_len = (remain_len>CHUNCK_SIZE)?CHUNCK_SIZE:remain_len;
  791. rt = send_data(h, data+offset, send_len, 500);
  792. if(rt!=AT_OK) {
  793. r = -1; break;
  794. }
  795. offset += send_len;
  796. }
  797. return r;
  798. }
  799. static int cell_sock_stat(sock_conn_t *conn)
  800. {
  801. int r;
  802. char buf[100];
  803. cell_handle_t *h=(cell_handle_t*)conn->h;
  804. if(h->stat==AT_STAT_UNINIT) {
  805. return -1;
  806. }
  807. r = send_cmd(h, AT_CMD_QISTATE, NULL, buf, sizeof(buf));
  808. if(r!=AT_OK) {
  809. return -1;
  810. }
  811. return 0;
  812. }
  813. static int cell_sock_connected(cell_handle_t *h)
  814. {
  815. return 0;
  816. }
  817. static int cell_send_sms(cell_handle_t *h, char *number, char *txt)
  818. {
  819. int r=-1,rt;
  820. char tmp[100],buf[200];
  821. if(h->stat==AT_STAT_UNINIT) {
  822. return -1;
  823. }
  824. sprintf(tmp, "=\"%s\"", number);
  825. r = send_cmd(h, AT_CMD_CMGS, tmp, buf, sizeof(buf));
  826. if(r!=AT_OK) {
  827. return -1;
  828. }
  829. snprintf(buf, sizeof(buf), "%s\x1a", txt);
  830. r = send_data(h, buf, strlen(buf), 5000);
  831. return r;
  832. }
  833. static int cell_send_call(cell_handle_t *h, char *number, void *data, int len)
  834. {
  835. int r;
  836. char buf[100];
  837. if(h->stat==AT_STAT_UNINIT) {
  838. return -1;
  839. }
  840. sprintf(buf, "%s", number);
  841. r = send_cmd(h, AT_CMD_ATD, NULL, buf, sizeof(buf));
  842. if(r!=AT_OK) {
  843. return -1;
  844. }
  845. return 0;
  846. }
  847. /////////////////////////////////////////////////////////////////////////
  848. static int conn_cfg(mqtt_conn_t *conn, char *item, char *value)
  849. {
  850. int r;
  851. char tmp[100],buf[200];
  852. cell_handle_t *h=(cell_handle_t*)conn->h;
  853. if(h->stat==AT_STAT_UNINIT) {
  854. return -1;
  855. }
  856. sprintf(tmp, "=\"%s\",%s", item, value);
  857. r = send_cmd(h, AT_CMD_QMTCFG, NULL, buf, sizeof(buf));
  858. if(r!=AT_OK) {
  859. return -1;
  860. }
  861. return 0;
  862. }
  863. static void conn_init(cell_handle_t *h, mqtt_conn_t *conn)
  864. {
  865. char buf[100];
  866. conn->h = h;
  867. conn->ver = MQTT_VER;
  868. conn->pdpId = conn->cliId+1; //1~15
  869. conn->msgId = 0;
  870. conn->qos = MQTT_QOS;
  871. conn->retain = 1;
  872. conn->will.flag = MQTT_WILL;
  873. conn->will.qos = MQTT_QOS;
  874. conn->will.retain = MQTT_RETAIN;
  875. strcpy(conn->will.topic, "");
  876. strcpy(conn->will.message, "");
  877. conn->will.message_len = strlen(conn->will.message);
  878. conn->timeout = 10; //1~60, default:5
  879. conn->retry_times = 3;
  880. conn->timeout_notice = 1;
  881. conn->clean_session = 1;
  882. conn->ssl_enable = 0;
  883. conn->ssl_ctx_id = 0;
  884. conn->msg_recv_mode = 0; //0: urc, 1: not urc
  885. conn->msg_len_enable = 1;
  886. conn->ping_interval = 5; //5~60, default:5
  887. conn->proto_chk_enable = 0; //0:no check 1:check
  888. conn->send_mode = 0; //0:string 1:hex
  889. conn->recv_mode = 0; //0:string 1:hex
  890. conn->view_mode = 0; //0:no echo 1:echo
  891. conn->edit_mode = 0; //0:timeout not quit 1:timeout quit
  892. conn->edit_time = 5; //1~120 seconds
  893. //conn->plat.product_key[64]; //aliyun used
  894. //conn->plat.device_name[64]; //aliyun used
  895. //conn->plat.device_secret[64]; //aliyun/ used
  896. //conn->plat.hw_time_enable; //huawei used
  897. //conn->plat.product_secret[64]; //huawei used
  898. //conn->plat.node_id[64];
  899. //conn->plat.product_id[64]; //onenet used
  900. //conn->plat.access_key[64]; //onenet used
  901. sprintf(buf, "%d,%d", conn->cliId, conn->ver);
  902. conn_cfg(conn, "version", buf);
  903. sprintf(buf, "%d,%d", conn->cliId, conn->pdpId);
  904. conn_cfg(conn, "pdpcid", buf);
  905. sprintf(buf, "%d,%d", conn->cliId, conn->keep_alive_time);
  906. conn_cfg(conn, "keepalive", buf);
  907. sprintf(buf, "%d,%d", conn->cliId, conn->clean_session);
  908. conn_cfg(conn, "session", buf);
  909. sprintf(buf, "%d,%d,%d,%d", conn->cliId, conn->timeout, conn->retry_times, conn->timeout_notice);
  910. conn_cfg(conn, "timeout", buf);
  911. sprintf(buf, "%d,%d,%d", conn->cliId, conn->ssl_enable, conn->ssl_ctx_id);
  912. conn_cfg(conn, "ssl", buf);
  913. sprintf(buf, "%d,%d", conn->cliId, conn->ping_interval);
  914. conn_cfg(conn, "qmtping", buf);
  915. //sprintf(buf, "%d,%d,%d,%d,\"%s\",\"%s\"", conn->cliId, conn->will.flag, conn->will.qos, conn->will.retain, conn->will.topic, conn->will.message);
  916. //conn_cfg(conn, "will", buf);
  917. //sprintf(buf, "%d,%d,%d,%d,\"%s\",%d", conn->cliId, conn->will.flag, conn->will.qos, conn->will.retain, conn->will.topic, conn->will.message_len);
  918. //conn_cfg(conn, "willex", buf);
  919. sprintf(buf, "%d,%d", conn->cliId, conn->send_mode);
  920. conn_cfg(conn, "send/mode", buf);
  921. sprintf(buf, "%d,%d,%d", conn->cliId, conn->msg_recv_mode, conn->msg_len_enable);
  922. conn_cfg(conn, "recv/mode", buf);
  923. sprintf(buf, "%d,%d", conn->cliId, conn->view_mode);
  924. conn_cfg(conn, "view/mode", buf);
  925. sprintf(buf, "%d,%d,%d", conn->cliId, conn->edit_mode, conn->edit_time);
  926. conn_cfg(conn, "edit/timeout", buf);
  927. sprintf(buf, "%d,%d", conn->cliId, conn->proto_chk_enable);
  928. conn_cfg(conn, "protocol/check", buf);
  929. sprintf(buf, "%d,%d,%d", conn->cliId, conn->send_mode, conn->recv_mode);
  930. conn_cfg(conn, "dataformat", buf);
  931. #if 0 //aliyun
  932. sprintf(buf, "%d,\"%s\",\"%s\",\"%s\"", conn->cliId, conn->plat.product_key, conn->plat.device_name, conn->plat.device_secret);
  933. conn_cfg(conn, "aliauth", buf);
  934. #endif
  935. #if 0 //huawei
  936. sprintf(buf, "%d,\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.device_secret);
  937. conn_cfg(conn, "hwauth", buf);
  938. sprintf(buf, "%d,\"%s\",\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.product_secret, conn->plat.node_id);
  939. conn_cfg(conn, "hwprodid", buf);
  940. #endif
  941. #if 0 //onenet
  942. sprintf(buf, "%d,\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.access_key);
  943. conn_cfg(conn, "onenet", buf);
  944. #endif
  945. }
  946. static int cell_test(cell_handle_t *h)
  947. {
  948. int r;
  949. cell_info_t info;
  950. cell_stat_t stat;
  951. cell_get_info(h, &info);
  952. while(1) {
  953. r = cell_get_stat(h, &stat);
  954. if(r==0) {
  955. LOGD("___dbm: %d ber: %d\n", stat.dbm, stat.ber);
  956. }
  957. else {
  958. LOGE("___ get stat failed\n");
  959. }
  960. sleep(3);
  961. }
  962. return 0;
  963. }
  964. /////////////////////////////////////////////////////////////////////////
  965. static int urc_handle(char *data, int len)
  966. {
  967. char *key=NULL;
  968. key = "+QIURC: ";
  969. if(strstr(data, key)) {
  970. }
  971. key = "+QMT";
  972. if(strstr(data, key)) {
  973. }
  974. return 0;
  975. }
  976. static void* cellular_thread(void *arg)
  977. {
  978. int r;
  979. uint32_t cnt=0;
  980. char rxbuf[BUF_LEN];
  981. thread_handle_t *th=(thread_handle_t*)arg;
  982. cell_handle_t *h=(cell_handle_t*)th->arg;
  983. port_init(h);
  984. while (th->quit==0) {
  985. cnt++;
  986. if(h->pcell->mode) {
  987. if(cnt%5==0) {
  988. cell_init(h);
  989. }
  990. if(cnt%5==0) {
  991. cell_get_stat(h, &h->all.stat);
  992. }
  993. }
  994. sleep(1);
  995. }
  996. cell_deinit(h);
  997. port_deinit(h);
  998. pthread_exit(NULL);
  999. }
  1000. #endif
  1001. int cellular_reset(void)
  1002. {
  1003. int r=-1;
  1004. cell_handle_t *h=&ceHandle;
  1005. #ifdef USE_CELL
  1006. r = cell_reset(h);
  1007. #endif
  1008. return r;
  1009. }
  1010. int cellular_init(void)
  1011. {
  1012. int r;
  1013. cell_handle_t *h=&ceHandle;
  1014. #ifdef USE_CELL
  1015. memset(h, 0, sizeof(cell_handle_t));
  1016. h->dm = &__globalDeviceManage;
  1017. h->pcell = &h->dm->_globalDevInfo.cell;
  1018. h->prod_id = h->dm->_globalDevInfo.product.id;
  1019. h->lck = lock_d_init();
  1020. thread_start(THREAD_ID_CELLULAR, cellular_thread, h);
  1021. while(1) sleep(10);
  1022. #endif
  1023. return 0;
  1024. }
  1025. int cellular_deinit(void)
  1026. {
  1027. cell_handle_t *h=&ceHandle;
  1028. #ifdef USE_CELL
  1029. lock_d_deinit(h->lck);
  1030. thread_stop(THREAD_ID_CELLULAR);
  1031. #endif
  1032. return 0;
  1033. }
  1034. int cellular_get_all(cell_all_t *all)
  1035. {
  1036. int r=-1;
  1037. cell_handle_t *h=&ceHandle;
  1038. #ifdef USE_CELL
  1039. if(all) {
  1040. *all = h->all;
  1041. r = 0;
  1042. }
  1043. #endif
  1044. return r;
  1045. }
  1046. int cellular_is_connected(void)
  1047. {
  1048. int r=0;
  1049. cell_handle_t *h=&ceHandle;
  1050. #ifdef USE_CELL
  1051. r = h->all.stat.linked;
  1052. #endif
  1053. return r;
  1054. }
  1055. int cellular_test(void)
  1056. {
  1057. cell_handle_t *h=&ceHandle;
  1058. #ifdef USE_CELL
  1059. cell_test(h);
  1060. #endif
  1061. return 0;
  1062. }