Cellular.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  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. #include "sqlite_handle.h"
  22. #if 0
  23. #define LOGE log_e
  24. #define LOGW log_w
  25. #define LOGD log_d
  26. #else
  27. #define LOGD printf
  28. #define LOGW printf
  29. #define LOGE printf
  30. #endif
  31. #define RETRY_TIMES 1
  32. #define BUF_LEN 2000
  33. #define AT_PORT "/dev/ttyUSB1"
  34. #define BAUD_RATE B115200
  35. #define QUERY_INTERVAL 1000 // 查询间隔,毫秒
  36. #define RSSI_DBM_MIN1 -113 // LTE、HSDPA网络最小信号值,对应RSSI值范围0-57
  37. #define RSSI_DBM_MIN2 -116 // TD-SCDMA网络最小信号值,对应RSSI值范围100-199
  38. #define CONTEXT_ID "1" //1~15
  39. #define CONNECT_ID "0" //0~11
  40. #define CONNECT_TIMEOUT "5"
  41. #define CHUNCK_SIZE 1460
  42. #define MQTT_VER 4 //3: v3.1 4: v3.1.1
  43. #define MQTT_WILL 0
  44. #define MQTT_RETAIN 0
  45. #define MQTT_MAX 6
  46. #define MQTT_QOS 2
  47. #define MQTT_CHUNCK_SIZE 1500
  48. #define SMS_SEND_MAX 100
  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. mqtt_conn_t mq_conn[MQTT_MAX];
  164. at_data_t data[AT_CMD_MAX];
  165. GlobalDeviceManager *dm;
  166. }cell_handle_t;
  167. static cell_handle_t ceHandle={0};
  168. static at_cmd_t all_cmd[AT_CMD_MAX]={
  169. //genernal cmd
  170. {"AT", "OK", "ERROR", 300}, //测试命令
  171. {"ATI", "OK", "ERROR", 300}, //显示MT的ID信息
  172. {"AT+GMI", "OK", "ERROR", 300}, //请求制造商信息
  173. {"AT+GMM", "OK", "ERROR", 300}, //请求TA型号ID
  174. {"AT+GMR", "OK", "ERROR", 300}, //请求TA固件版本ID
  175. {"AT+CGMI", "OK", "ERROR", 300}, //同GMI
  176. {"AT+CGMM", "OK", "ERROR", 300}, //同GMM
  177. {"AT+CGMR", "OK", "ERROR", 300}, //同GMR
  178. {"AT+GSN", "OK", "ERROR", 300}, //请求国际移动设备识别码(IMEI)
  179. {"AT+CGSN", "OK", "ERROR", 300}, //同GSN
  180. {"AT&F", "OK", "ERROR", 300}, //重置AT命令设置为出厂设置
  181. {"AT&V", "OK", "ERROR", 300}, //显示当前配置
  182. {"AT&W", "OK", "ERROR", 300}, //存储当前设置到用户自定义配置文件
  183. {"ATZ", "OK", "ERROR", 300}, //从用户定义配置文件还原所有AT命令设置
  184. {"ATQ", "OK", "ERROR", 300}, //设置结果码回显模式
  185. {"ATV", "OK", "ERROR", 300}, //TA响应格式
  186. {"ATE0", "OK", "ERROR", 300}, //设置命令回显模式
  187. {"ATS3", "OK", "ERROR", 300}, //设置命令行终止符
  188. {"ATS4", "OK", "ERROR", 300}, //设置响应格式字符
  189. {"ATS5", "OK", "ERROR", 300}, //设置命令行编辑字符
  190. {"ATX", "OK", "ERROR", 300}, //设置CONNECT结果码格式和检测呼叫进程
  191. {"AT+CFUN", "OK", "ERROR", 15000}, //设置功能模式
  192. {"AT+CMEE", "OK", "ERROR", 300}, //设置错误讯息格式
  193. {"AT+CSCS", "OK", "ERROR", 300}, //选择TE字符集
  194. {"AT+QURCCFG", "OK", "ERROR", 300}, //配置URC指示选项
  195. {"AT+QPPPDROP", "OK", "ERROR", 300}, //终止PPP连接
  196. //serial cmd
  197. {"AT&C", "OK", "ERROR", 300}, //设置DCD信号模式
  198. {"AT&D", "OK", "ERROR", 300}, //设置DTR信号模式
  199. {"AT+IFC", "OK", "ERROR", 300}, //设置串口的流控方式
  200. {"AT+IPR", "OK", "ERROR", 300}, //设置固定的串口通信波特率
  201. //status cmd
  202. {"AT+CPAS", "OK", "ERROR", 300}, //查询ME活动状态
  203. {"AT+CEER", "OK", "ERROR", 300}, //上报扩展错误
  204. {"AT+QCFG", "OK", "ERROR", 300}, //扩展命令, 较多子命令
  205. {"AT+QINDCFG", "OK", "ERROR", 300}, //控制URC上报
  206. //sim cmd
  207. {"AT+CIMI", "OK", "ERROR", 300}, //查询IMSI
  208. {"AT+CLCK", "OK", "ERROR", 5000}, //功能锁定
  209. {"AT+CPIN", "OK", "ERROR", 5000}, //PIN管理
  210. {"AT+CPWD", "OK", "ERROR", 5000}, //修改密码
  211. {"AT+CSIM", "OK", "ERROR", 300}, //通用(U)SIM卡访问
  212. {"AT+CRSM", "OK", "ERROR", 300}, //(U)SIM卡受限访问
  213. {"AT+QCCID", "OK", "ERROR", 300}, //查询ICCID
  214. {"AT+QPINC", "OK", "ERROR", 300}, //获取PIN剩余重试次数
  215. {"AT+QINISTAT", "OK", "ERROR", 300}, //查询(U)SIM卡初始化状态
  216. {"AT+QSIMDET", "OK", "ERROR", 300}, //(U)SIM卡检测
  217. {"AT+QSIMSTAT", "OK", "ERROR", 300}, //(U)SIM卡插拔状态上报
  218. {"AT+QDSIM", "OK", "ERROR", 50000}, //选择(U)SIM卡
  219. //network service cmd
  220. {"AT+COPS", "OK", "ERROR", 300000}, //选择运营商
  221. {"AT+CREG", "OK", "ERROR", 300}, //CS域网络注册状态
  222. {"AT+CSQ", "OK", "ERROR", 300}, //查询信号强度
  223. {"AT+CPOL", "OK", "ERROR", 300}, //配置首选运营商列表
  224. {"AT+COPN", "OK", "ERROR", 300}, //查询运营商名称列表
  225. {"AT+CTZU", "OK", "ERROR", 300}, //自动更新时区
  226. {"AT+CTZR", "OK", "ERROR", 300}, //上报时区变化
  227. {"AT+QLTS", "OK", "ERROR", 500}, //获取通过网络同步的最新时间
  228. {"AT+QNWINFO", "OK", "ERROR", 300}, //查询网络信息
  229. //call cmd
  230. {"ATA", "OK", "ERROR", 90000}, //呼叫应答
  231. {"ATD", "OK", "ERROR", 5000}, //发起呼叫
  232. {"AT+COLP", "OK", "ERROR", 15000}, //被叫线路识别显示
  233. {"AT+ATH", "OK", "ERROR", 90000}, //挂断现有连接
  234. {"AT+CHUP", "OK", "ERROR", 90000}, //挂断语音通话
  235. {"+++", "OK", "ERROR", 300}, //从数据模式切换至命令模式
  236. {"AT+ATO", "OK", "ERROR", 300}, //从命令模式切换至数据模式
  237. {"AT+ATSO", "OK", "ERROR", 300}, //设置自动应答前振铃次数
  238. {"AT+CLCC", "OK", "ERROR", 300}, //查询当前 ME 呼叫
  239. {"AT^DSCI", "OK", "ERROR", 300}, //指示呼叫状态
  240. //phonebook cmd
  241. {"AT+CNUM", "OK", "ERROR", 300}, //查询本机号码
  242. {"AT+CPBF", "OK", "ERROR", 300}, //搜索电话本
  243. {"AT+CPBR", "OK", "ERROR", 300}, //读取电话本条目
  244. {"AT+CPBS", "OK", "ERROR", 300}, //设置电话本存储器
  245. {"AT+CPBW", "OK", "ERROR", 300}, //写入电话本
  246. //sms cmd
  247. {"AT+CSMS", "OK", "ERROR", 300}, //选择短消息服务类型
  248. {"AT+CMGF", "OK", "ERROR", 300}, //配置短消息模式
  249. {"AT+CSCA", "OK", "ERROR", 300}, //设置短消息服务中心地址
  250. {"AT+CPMS", "OK", "ERROR", 300}, //配置短消息首选存储位置
  251. {"AT+CMGD", "OK", "ERROR", 300}, //删除短消息
  252. {"AT+CMGL", "OK", "ERROR", 300}, //按状态读取短消息
  253. {"AT+CMGR", "OK", "ERROR", 300}, //按照索引读取短消息
  254. {"AT+CMGS", ">", "ERROR", 120000}, //发送短消息
  255. {"AT+CMMS", "OK", "ERROR", 300}, //发送多条短消息
  256. {"AT+CMGW", "OK", "ERROR", 300}, //存储短消息
  257. {"AT+CMSS", "OK", "ERROR", 300}, //从存储器发送短消息
  258. {"AT+CNMA", "OK", "ERROR", 300}, //新短消息确认
  259. {"AT+CNMI", "OK", "ERROR", 300}, //设置短消息上报方式
  260. {"AT+CSCB", "OK", "ERROR", 300}, //选择小区广播消息类型
  261. {"AT+CSDH", "OK", "ERROR", 300}, //设置文本模式下参数显示
  262. {"AT+CSMP", "OK", "ERROR", 300}, //设置短消息文本模式参数
  263. {"AT+QCMGS", "OK", "ERROR", 120000}, //发送级联短消息
  264. {"AT+QCMGR", "OK", "ERROR", 300}, //读取级联短消息
  265. //gps cmd
  266. {"AT+QGPSCFG", "OK", "ERROR", 300}, //配置GNSS
  267. {"AT+QGPSDEL", "OK", "ERROR", 300}, //删除辅助数据
  268. {"AT+QGPS", "OK", "ERROR", 300}, //打开GNSS
  269. {"AT+QGPSEND", "OK", "ERROR", 300}, //关闭GNSS
  270. {"AT+QGPSLOC", "OK", "ERROR", 300}, //获取定位信息
  271. {"AT+QGPSNMEA", "OK", "ERROR", 300}, //获取指定的NMEA语句
  272. {"AT+QAGPS", "OK", "ERROR", 300}, //启用/禁用AGPS
  273. //tcp/udp cmd
  274. {"AT+QICFG", "OK", "ERROR", 300}, //配置可选参数
  275. {"AT+QICSGP", "OK", "ERROR", 300}, //配置TCP/IP场景参数
  276. {"AT+QIACT", "OK", "ERROR", 10000}, //激活PDP场景
  277. {"AT+QIDEACT", "OK", "ERROR", 10000}, //去激活PDP场景
  278. {"AT+QIOPEN", "OK", "ERROR", 300}, //打开Socket服务
  279. {"AT+QICLOSE", "OK", "ERROR", 300}, //关闭Socket服务
  280. {"AT+QISTATE", "OK", "ERROR", 300}, //查询Socket服务状态
  281. {"AT+QISEND", ">", "ERROR", 300}, //发送数据
  282. {"AT+QIRD", "OK", "ERROR", 300}, //读取收到的TCP/IP数据
  283. {"AT+QISENDEX", "OK", "ERROR", 300}, //发送16进制字符串数据
  284. {"AT+QISWTMD", "OK", "ERROR", 300}, //切换数据访问模式
  285. {"AT+QNTP", "OK", "ERROR", 300}, //进行远程服务器Ping操作
  286. {"AT+QPING", "+QPING", "ERROR", 3000}, //使用NTP服务器同步本地时间
  287. {"AT+QIDNSCFG", "OK", "ERROR", 300}, //配置DNS服务器地址
  288. {"AT+QIDNSGIP", "OK", "ERROR", 300}, //用域名获取IP地址
  289. {"AT+QISDE", "OK", "ERROR", 300}, //控制是否回显AT+QISEND要发送的
  290. {"AT+QIGETERROR", "OK", "ERROR", 300}, //查询上一个AT命令错误代码
  291. //mqtt cmd
  292. {"AT+QMTCFG", "OK", "ERROR", 300}, //配置MQTT可选参数
  293. {"AT+QMTOPEN", "OK", "ERROR", 300}, //打开MQTT客户端网络
  294. {"AT+QMTCLOSE", "OK", "ERROR", 300}, //关闭MQTT客户端网络
  295. {"AT+QMTCONN", "OK", "ERROR", 300}, //连接客户端到MQTT服务器
  296. {"AT+QMTDISC", "OK", "ERROR", 300}, //客户端断开与MQTT服务器的连接
  297. {"AT+QMTSUB", "OK", "ERROR", 300}, //订阅主题
  298. {"AT+QMTUNS", "OK", "ERROR", 300}, //退订主题
  299. {"AT+QMTPUBEX", ">", "ERROR", 300}, //发布消息
  300. {"AT+QMTRECV", "OK", "ERROR", 300}, //从缓存中读取消息
  301. //domain cmd
  302. {"AT+CGATT", "OK", "ERROR", 140000}, //PS域附着或去附着
  303. {"AT+CGDCONT", "OK", "ERROR", 3000}, //定义PDP上下文
  304. {"AT+CGQREQ", "OK", "ERROR", 300}, //服务质量参数请求
  305. {"AT+CGQMIN", "OK", "ERROR", 300}, //服务质量参数(最低可接受的)
  306. {"AT+CGEQREQ", "OK", "ERROR", 300}, //3G服务质量参数(请求的)
  307. {"AT+CGEQMIN", "OK", "ERROR", 300}, //3G 服务质量参数(最低可接受的)
  308. {"AT+CGACT", "OK", "ERROR", 150000}, //PDP上下文激活/去激活
  309. {"AT+CGDATA", "OK", "ERROR", 300}, //进入数据传输状态
  310. {"AT+CGPADDR", "OK", "ERROR", 300}, //显示PDP地址
  311. {"AT+CGCLASS", "OK", "ERROR", 300}, //GPRS移动站点等级
  312. {"AT+CGEREG", "OK", "ERROR", 300}, //PS域网络注册状态
  313. {"AT+CGEREP", "OK", "ERROR", 300}, //分组域事件上报
  314. {"AT+CGSMS", "OK", "ERROR", 300}, //选择MO短消息服务
  315. {"AT+CEREG", "OK", "ERROR", 300}, //EPS网络注册状态
  316. {"AT+QGDCNT", "OK", "ERROR", 300}, //流量统计
  317. {"AT+QAUGDCNT", "OK", "ERROR", 300}, //自动保存流量统计
  318. {"AT+QNETDEVCTL", "OK", "ERROR", 2000}, //连接USB网卡
  319. {"AT+QCEERCATCFG", "OK", "ERROR", 300}, //配置2G/4G下AT+CEER返回格式
  320. //hardware cmd
  321. {"AT+QPOWD", "OK", "ERROR", 300}, //关闭模块
  322. {"AT+CCLK", "OK", "ERROR", 300}, //实时时钟
  323. {"AT+QSCLK", "OK", "ERROR", 300}, //禁用/启用睡眠模式
  324. };
  325. at_para_t init_para[]={
  326. {AT_CMD_AT, ""},
  327. {AT_CMD_ATE, "0"},
  328. {AT_CMD_CPIN, "?"},
  329. {AT_CMD_CREG, "?"},
  330. //{AT_CMD_CGDCONT, ""},
  331. {AT_CMD_QICSGP, ""},
  332. {AT_CMD_QIDEACT, "="CONTEXT_ID},
  333. {AT_CMD_QIACT, "="CONTEXT_ID},
  334. {AT_CMD_QCFG, "=\"usbnet\",1"}, //set module to ecm mode
  335. {AT_CMD_QNETDEVCTL, "=3,"CONTEXT_ID",0"},
  336. {AT_CMD_CMGF, "=1"},
  337. {AT_CMD_CSCS, "=GSM"},
  338. #ifdef GNSS_ENABLE
  339. {AT_CMD_QGPS, "=1"},
  340. {AT_CMD_QGPSCFG, "=\"gnssconfig\",5"},
  341. {AT_CMD_QGPSCFG, "=\"gpsnmeatype\",1"},
  342. #endif
  343. {-1, ""},
  344. };
  345. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  346. static at_cmd_t* data_of(int cmd)
  347. {
  348. return &all_cmd[cmd];
  349. }
  350. static int rssi_to_dbm(int rssi)
  351. {
  352. int dbm=0;
  353. if(rssi>=0 && rssi<57) {
  354. dbm = RSSI_DBM_MIN1+rssi*2;
  355. }
  356. else if(rssi>=100 && rssi<199) {
  357. dbm = RSSI_DBM_MIN2+rssi-100;
  358. }
  359. return dbm;
  360. }
  361. static char* str_get(char *buf, char c)
  362. {
  363. int i,len=strlen(buf);
  364. for(i=0; i<len; i++) {
  365. if(buf[i]==c) return buf+i;
  366. }
  367. return buf;
  368. }
  369. static int mem_find(char *buf, int len, char *str)
  370. {
  371. int i,slen=strlen(str);
  372. for(i=0; i<=len-slen; i++) {
  373. if(memcmp(buf+i, str, slen)==0) {
  374. return 0;
  375. }
  376. }
  377. return -1;
  378. }
  379. static void mem_print(char *buf, int len)
  380. {
  381. int i;
  382. for(i=0; i<len; i++) {
  383. LOGD("%d, ", buf[i]);
  384. if((i+1)%16==0) LOGD("\n");
  385. }
  386. LOGD("\n");
  387. }
  388. static int nic_set(int nic, int on)
  389. {
  390. char tmp[200],path[100];
  391. const char *ifname=sys_get_ifname(nic);
  392. sprintf(path, "/var/run/udhcpc_%s.pid", ifname);
  393. sprintf(tmp, "kill $(cat %s)", path);
  394. system(tmp);
  395. sprintf(tmp, "ip link set dev %s %s", ifname, on?"up":"down");
  396. system(tmp);
  397. if(on) {
  398. sprintf(tmp, "udhcpc -R -b -i %s -p %s ", ifname, path);
  399. system(tmp);
  400. }
  401. return 0;
  402. }
  403. static int cell_reset(cell_handle_t *h);
  404. static int cell_sock_init(cell_handle_t *h);
  405. static int cell_get_info(cell_handle_t *h, cell_info_t *info);
  406. static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat);
  407. static void conn_init(cell_handle_t *h, mqtt_conn_t *conn);
  408. //////////////////////////////////////////////////////////////////////////////////
  409. static int port_setup(cell_handle_t *h, int speed);
  410. static void msleep(int ms)
  411. {
  412. usleep(ms*1000);
  413. }
  414. static void reset_var( cell_handle_t *h)
  415. {
  416. h->stat=AT_STAT_UNINIT;
  417. h->all.stat.dbm = 0;
  418. h->all.stat.ber = 0;
  419. h->all.stat.nsta = 0;
  420. h->all.stat.altitude = 0;
  421. sprintf(h->all.stat.latitude, "-");
  422. sprintf(h->all.stat.longitude, "-");
  423. h->all.stat.utctime = 0;
  424. h->all.stat.linked = 0;
  425. sprintf(h->all.info.apn, "-");
  426. sprintf(h->all.info.cnum, "-");
  427. sprintf(h->all.info.imsi, "-");
  428. sprintf(h->all.info.imei, "-");
  429. sprintf(h->all.info.isp, "-");
  430. }
  431. static int port_init(cell_handle_t *h)
  432. {
  433. h->fd = open(AT_PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);
  434. if(h->fd<0) {
  435. return -1;
  436. }
  437. port_setup(h, BAUD_RATE);
  438. return 0;
  439. }
  440. static int port_deinit(cell_handle_t *h)
  441. {
  442. if(h->fd<0) {
  443. return -1;
  444. }
  445. int r=close(h->fd);
  446. h->fd=-1;
  447. sleep(2);
  448. return r;
  449. }
  450. static int port_read(cell_handle_t *h, void *data, int len)
  451. {
  452. return read(h->fd, data, len);
  453. }
  454. static int port_write(cell_handle_t *h, void *data, int len)
  455. {
  456. return write(h->fd, data, len);
  457. }
  458. static int port_setup(cell_handle_t *h, int speed)
  459. {
  460. struct termios tty;
  461. if (tcgetattr(h->fd, &tty) != 0) {
  462. LOGE("Read 4G Module tcgetattr!!!\n");
  463. return -1;
  464. }
  465. cfsetospeed(&tty, speed);
  466. cfsetispeed(&tty, speed);
  467. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
  468. tty.c_iflag &= ~IGNBRK; // disable break processing
  469. tty.c_lflag = 0; // no signaling chars, no echo,
  470. // no canonical processing
  471. tty.c_oflag = 0; // no remapping, no delays
  472. tty.c_cc[VMIN] = 0; // read doesn't block
  473. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  474. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  475. tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
  476. // enable reading
  477. tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
  478. tty.c_cflag |= 0;
  479. tty.c_cflag &= ~CSTOPB;
  480. tty.c_cflag &= ~CRTSCTS;
  481. if (tcsetattr(h->fd, TCSANOW, &tty) != 0) {
  482. LOGE("Read 4G Module tcsetattr!!!\n");
  483. return -1;
  484. }
  485. return 0;
  486. }
  487. static int port_wait(cell_handle_t *h, int timeout)
  488. {
  489. int r;
  490. char buf[100];
  491. fd_set fset;
  492. struct timeval tv;
  493. tv.tv_sec = timeout/1000;
  494. tv.tv_usec = (timeout%1000)*1000;
  495. if(h->fd < 0)
  496. return -1;
  497. FD_ZERO(&fset); FD_SET(h->fd, &fset);
  498. r = select(h->fd + 1, &fset, NULL, NULL, &tv);
  499. if (r<=0) {
  500. if(r<0) {
  501. //LOGE("port_wait error %s\n", strerror(errno));
  502. }
  503. else {
  504. //LOGE("port_wait timeout\n");
  505. }
  506. }
  507. return r;
  508. }
  509. static int port_recv(cell_handle_t *h, char *buf, int buflen, int timeout)
  510. {
  511. int r;
  512. r = port_wait(h, timeout);
  513. if(r<0) {
  514. return -1;
  515. }
  516. r = port_read(h, buf, buflen);
  517. if(r<=0) {
  518. return -1;
  519. }
  520. return r;
  521. }
  522. static int send_cmd(cell_handle_t *h, int cmd, char *para, char *rcv, int rcvlen)
  523. {
  524. int i,r,l,tout,finish=0;
  525. int tlen=0,buflen,wait_step=10;
  526. char buf[BUF_LEN],*pbuf;
  527. at_cmd_t *pcmd=data_of(cmd);
  528. sprintf(buf, "%s%s\r", pcmd->cmd, para?para:"");
  529. LOGD("[tx]: %s\n", buf);
  530. for (i = 0; i < RETRY_TIMES; i++)
  531. {
  532. tout = 0;
  533. port_write(h, buf, strlen(buf));
  534. while (tout < pcmd->timeout)
  535. {
  536. l = port_wait(h, wait_step);
  537. if (l > 0)
  538. {
  539. if (rcv && rcvlen > 0)
  540. {
  541. pbuf = rcv;
  542. buflen = rcvlen;
  543. }
  544. else
  545. {
  546. pbuf = buf;
  547. buflen = sizeof(buf);
  548. }
  549. l = port_read(h, pbuf + tlen, buflen);
  550. if (l > 0)
  551. {
  552. tlen += l;
  553. pbuf[tlen] = 0;
  554. LOGD("[rx]: %s\n", pbuf);
  555. if (pcmd->ok && strlen(pcmd->ok) && strstr(pbuf, pcmd->ok))
  556. {
  557. return AT_OK;
  558. }
  559. if (pcmd->err && strlen(pcmd->err) && strstr(pbuf, pcmd->err))
  560. {
  561. // 指令不通则重置模块
  562. if (cmd != AT_CMD_CFUN && cmd != AT_CMD_QIDEACT )
  563. {
  564. cell_reset(h);
  565. }
  566. return AT_ERR;
  567. }
  568. }
  569. }
  570. tout += wait_step;
  571. usleep(1000);
  572. }
  573. }
  574. //AT不通重启串口
  575. if (cmd != AT_CMD_QIDEACT && cmd != AT_CMD_CFUN )
  576. {
  577. port_deinit(h);
  578. port_init(h);
  579. }
  580. return AT_TOUT;
  581. }
  582. static int send_data(cell_handle_t *h, void *data, int len, int timeout)
  583. {
  584. char buf[100];
  585. int l,tlen=0,tout=0,wait_step=10;
  586. l = port_write(h, data, len);
  587. if(l==len) {
  588. while(tout<timeout) {
  589. l = port_wait(h, wait_step);
  590. if(l>0) {
  591. l = port_read(h, buf, sizeof(buf));
  592. if(l>0) {
  593. tlen += l; buf[tlen] = 0;
  594. if(strstr(buf, "OK")) {
  595. return AT_OK;
  596. }
  597. else if(strstr(buf, "ERROR")) {
  598. return AT_ERR;
  599. }
  600. }
  601. }
  602. tout += wait_step;
  603. }
  604. }
  605. return AT_TOUT;
  606. }
  607. ////////////////////////////////////////////////////////////////////////////
  608. static int cell_reset(cell_handle_t *h)
  609. {
  610. reset_var(h);
  611. int r;
  612. r = send_cmd(h, AT_CMD_CFUN, "=1,1", NULL, 0);
  613. // if(r==AT_OK) {
  614. // h->stat = AT_STAT_UNINIT;
  615. //}
  616. sleep(5);//等待模块复位完成
  617. return (r==AT_OK)?0:-1;
  618. }
  619. static int cell_init(cell_handle_t *h)
  620. {
  621. int i,r;
  622. at_para_t *p=init_para;
  623. if(h->stat==AT_STAT_INITED) {
  624. return 0;
  625. }
  626. for(i=0;; i++) {
  627. if(p[i].cmd==-1) {
  628. break;
  629. }
  630. if(p[i].cmd==AT_CMD_CGDCONT) {
  631. sprintf(p[i].para, "=%s,\"IPV4V6\",\"%s\"", CONTEXT_ID, h->pcell->apn);
  632. }
  633. else if(p[i].cmd==AT_CMD_QICSGP) {
  634. sprintf(p[i].para, "=%s,3,\"%s\",\"%s\",\"%s\"", CONTEXT_ID, h->pcell->apn,h->pcell->username,h->pcell->password);
  635. }
  636. r = send_cmd(h, p[i].cmd, p[i].para, NULL, 0);
  637. if(r!=AT_OK) {
  638. break;
  639. }
  640. }
  641. if(r==0) {
  642. h->stat = AT_STAT_INITED;
  643. cell_get_info(h, &h->all.info);
  644. }
  645. return r?-1:0;
  646. }
  647. static int cell_deinit(cell_handle_t *h)
  648. {
  649. int r=-1;
  650. char buf[400];
  651. r = send_cmd(h, AT_CMD_QIDEACT, "="CONTEXT_ID, buf, sizeof(buf));
  652. if(r==AT_OK) {
  653. return r;
  654. }
  655. return r;
  656. }
  657. static int cell_get_info(cell_handle_t *h, cell_info_t *info)
  658. {
  659. int r;
  660. char buf[400];
  661. if(h->stat==AT_STAT_UNINIT) {
  662. return -1;
  663. }
  664. memset(info, 0, sizeof(cell_info_t));
  665. // r = send_cmd(h, AT_CMD_CNUM, NULL, buf, sizeof(buf));
  666. // if(r==AT_OK) {
  667. // r = sscanf(str_get(buf,'+'), "+CNUM: %*s,%s,%*d", info->cnum);
  668. // }
  669. r = send_cmd(h, AT_CMD_GSN, NULL, buf, sizeof(buf));
  670. if(r==AT_OK) {
  671. r = sscanf(str_get(buf,'+'), "%s", info->imei);
  672. }
  673. r = send_cmd(h, AT_CMD_CIMI, NULL, buf, sizeof(buf));
  674. if(r==AT_OK) {
  675. r = sscanf(str_get(buf,'+'), "%s", info->imsi);
  676. }
  677. r = send_cmd(h, AT_CMD_CGDCONT, "?", buf, sizeof(buf));
  678. if(r==AT_OK) {
  679. r = sscanf(str_get(buf,'+'), "+CGDCONT: %*d,\"%*[^\"]\",\"%[^\"]", info->apn);
  680. }
  681. r = send_cmd(h, AT_CMD_COPS, "?", buf, sizeof(buf));
  682. if(r==AT_OK) {
  683. r = sscanf(str_get(buf,'+'), "+COPS: %*d,%*d,\"%[^\"]", info->isp);
  684. }
  685. LOGD("___info->apn: %s\n", info->apn);
  686. LOGD("___info->isp: %s\n", info->isp);
  687. LOGD("___info->imei: %s\n", info->imei);
  688. LOGD("___info->imsi: %s\n", info->imsi);
  689. LOGD("___info->cnum: %s\n", info->cnum);
  690. return 0;
  691. }
  692. static int cell_get_sig(cell_handle_t *h, cell_stat_t *stat)
  693. {
  694. char buf[200];
  695. int rssi,ber,r=-1;
  696. if(h->stat==AT_STAT_UNINIT) {
  697. return -1;
  698. }
  699. r = send_cmd(h, AT_CMD_CSQ, NULL, buf, sizeof(buf));
  700. if (r != AT_OK)
  701. {
  702. return -1;
  703. }
  704. r = sscanf(str_get(buf, '+'), "%*s %d,%d", &rssi, &ber);
  705. if (r < 2)
  706. {
  707. return -1;
  708. }
  709. stat->dbm = rssi_to_dbm(rssi);
  710. stat->ber = ber;
  711. return 0;
  712. }
  713. static int cell_get_gnss(cell_handle_t *h, cell_stat_t *stat)
  714. {
  715. int r;
  716. char buf[200],utc[32],date[32];
  717. #ifdef GNSS_ENABLE
  718. if(h->stat==AT_STAT_UNINIT) {
  719. return -1;
  720. }
  721. r = send_cmd(h, AT_CMD_QGPSLOC, "=0", buf, sizeof(buf));
  722. if(r!=AT_OK) {
  723. return -1;
  724. }
  725. 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);
  726. if(r<6) {
  727. return -1;
  728. }
  729. //
  730. #endif
  731. return 0;
  732. }
  733. static int cell_get_ping(cell_handle_t *h, cell_stat_t *stat)
  734. {
  735. int r,ret;
  736. char buf[200],utc[32],date[32];
  737. if(h->stat==AT_STAT_UNINIT) {
  738. return -1;
  739. }
  740. r = send_cmd(h, AT_CMD_QPING, "=1,\"www.baidu.com\",3,2,", buf, sizeof(buf));
  741. if(r!=AT_OK) {
  742. stat->linked = 0;
  743. return -1;
  744. }
  745. r = sscanf(str_get(buf,'+'), "+QPING: %d", &ret);
  746. if(r<1) {
  747. stat->linked = 0;
  748. return -1;
  749. }
  750. LOGD("QPING: %d\n", ret);
  751. if(ret==0) {
  752. stat->linked = 1;
  753. }
  754. else {
  755. stat->linked = 0;
  756. }
  757. return 0;
  758. }
  759. static int cell_get_time(cell_handle_t *h, cell_stat_t *stat)
  760. {
  761. int r,offset;
  762. char opr,buf[200],date[24],time[24];
  763. struct tm utc_tm;
  764. if(h->stat==AT_STAT_UNINIT) {
  765. return -1;
  766. }
  767. r = send_cmd(h, AT_CMD_QLTS, "=1", buf, sizeof(buf));
  768. if(r!=AT_OK) {
  769. return -1;
  770. }
  771. 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);
  772. if(r<6) {
  773. return -1;
  774. }
  775. stat->utctime = mktime2(&utc_tm, 0);
  776. //print_time("999", &utc_tm);
  777. return 0;
  778. }
  779. static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat)
  780. {
  781. if(h->stat==AT_STAT_UNINIT) {
  782. return -1;
  783. }
  784. cell_get_sig(h, stat);
  785. cell_get_gnss(h, stat);
  786. cell_get_ping(h, stat);
  787. cell_get_time(h, stat);
  788. return 0;
  789. }
  790. static int cell_sock_conn(cell_handle_t *h, int sock, char *host, uint16_t port, sock_conn_t *conn)
  791. {
  792. int r;
  793. char buf[200];
  794. if(h->stat==AT_STAT_UNINIT) {
  795. return -1;
  796. }
  797. sprintf(buf, "=%s,%s,%s,“%s”,%d,0,1", CONTEXT_ID, CONNECT_ID, (sock==SOCK_TCP)?"TCP":"UDP", host, port);
  798. r = send_cmd(h, AT_CMD_QIOPEN, buf, NULL, 0);
  799. if(r!=AT_OK) {
  800. return -1;
  801. }
  802. conn->h = h;
  803. conn->sock = sock;
  804. conn->cont_id = atoi(CONTEXT_ID);
  805. conn->conn_id = atoi(CONNECT_ID);
  806. return 0;
  807. }
  808. static int cell_sock_disconn(sock_conn_t *conn)
  809. {
  810. int r;
  811. char buf[200];
  812. cell_handle_t *h=(cell_handle_t*)conn->h;
  813. if(h->stat==AT_STAT_UNINIT) {
  814. return -1;
  815. }
  816. sprintf(buf, "=%s,%s", CONNECT_ID, CONNECT_TIMEOUT);
  817. r = send_cmd(h, AT_CMD_QICLOSE, buf, NULL, 0);
  818. if(r!=AT_OK) {
  819. return -1;
  820. }
  821. conn->h = NULL;
  822. conn->sock = -1;
  823. conn->cont_id = -1;
  824. conn->conn_id = -1;
  825. return 0;
  826. }
  827. static int cell_sock_send(sock_conn_t *conn, void *data, int len)
  828. {
  829. int r=0,rt,offset=0,remain_len,send_len;
  830. char buf[100],*pd=(char*)data;
  831. cell_handle_t *h=(cell_handle_t*)conn->h;
  832. if(h->stat==AT_STAT_UNINIT) {
  833. return -1;
  834. }
  835. sprintf(buf, "=%d,%d", conn->conn_id, len);
  836. rt = send_cmd(h, AT_CMD_QISEND, buf, NULL, 0);
  837. if(rt!=AT_OK) {
  838. return -1;
  839. }
  840. while(offset<len) {
  841. remain_len = len - offset;
  842. send_len = (remain_len>CHUNCK_SIZE)?CHUNCK_SIZE:remain_len;
  843. rt = send_data(h, data+offset, send_len, 500);
  844. if(rt!=AT_OK) {
  845. r = -1; break;
  846. }
  847. offset += send_len;
  848. }
  849. return r;
  850. }
  851. static int cell_sock_stat(sock_conn_t *conn)
  852. {
  853. int r;
  854. char buf[100];
  855. cell_handle_t *h=(cell_handle_t*)conn->h;
  856. if(h->stat==AT_STAT_UNINIT) {
  857. return -1;
  858. }
  859. r = send_cmd(h, AT_CMD_QISTATE, NULL, buf, sizeof(buf));
  860. if(r!=AT_OK) {
  861. return -1;
  862. }
  863. return 0;
  864. }
  865. static int cell_sock_connected(cell_handle_t *h)
  866. {
  867. return 0;
  868. }
  869. static mqtt_conn_t* get_mqtt_conn_free(cell_handle_t *h)
  870. {
  871. int i;
  872. for(i=0; i<MQTT_MAX; i++) {
  873. if(h->mq_conn[i].h==NULL) {
  874. return &h->mq_conn[i];
  875. }
  876. }
  877. return NULL;
  878. }
  879. static int cell_mqtt_init(cell_handle_t *h)
  880. {
  881. int i;
  882. at_cmd_t *pcmd=NULL;
  883. for(i=0; i<MQTT_MAX; i++) {
  884. h->mq_conn[i].cliId = i;
  885. }
  886. return 0;
  887. }
  888. static int cell_mqtt_conn(cell_handle_t *h, mqtt_para_t *para)
  889. {
  890. int r;
  891. char tmp[200],buf[100];
  892. mqtt_conn_t *conn=NULL;
  893. conn = get_mqtt_conn_free(h);
  894. if(!conn) {
  895. return -1;
  896. }
  897. conn_init(h, conn);
  898. sprintf(tmp, "=%d,\"%s\",%d", conn->cliId, para->host, para->port);
  899. r = send_cmd(h, AT_CMD_QMTOPEN, tmp, buf, sizeof(buf));
  900. if(r!=AT_OK) {
  901. return -1;
  902. }
  903. sprintf(tmp, "=%d,\"mqtt%d\",\"%s\",\"%s\"", conn->cliId, conn->cliId, para->username, para->password);
  904. r = send_cmd(h, AT_CMD_QMTCONN, tmp, buf, sizeof(buf));
  905. if(r!=AT_OK) {
  906. return -1;
  907. }
  908. conn->h = h;
  909. conn->qos = 2;
  910. conn->retain = 0;
  911. return 0;
  912. }
  913. static int cell_mqtt_disconn(mqtt_conn_t *conn)
  914. {
  915. int r;
  916. char tmp[100],buf[100];
  917. at_cmd_t *pcmd=NULL;
  918. sprintf(tmp, "=%d", conn->cliId);
  919. r = send_cmd(conn->h, AT_CMD_QMTDISC, tmp, buf, sizeof(buf));
  920. if(r!=AT_OK) {
  921. return -1;
  922. }
  923. r = send_cmd(conn->h, AT_CMD_QMTCLOSE, tmp, buf, sizeof(buf));
  924. if(r!=AT_OK) {
  925. return -1;
  926. }
  927. conn->msgId = 0;
  928. conn->h = NULL;
  929. return 0;
  930. }
  931. static int cell_mqtt_sub(mqtt_conn_t *conn, char *topic)
  932. {
  933. int r;
  934. char buf[500];
  935. at_cmd_t *pcmd=NULL;
  936. sprintf(buf, "=%d, %d, \"%s\",%d", conn->cliId, conn->msgId, topic, conn->qos);
  937. r = send_cmd(conn->h, AT_CMD_QMTSUB, buf, NULL, 0);
  938. if(r!=AT_OK) {
  939. return -1;
  940. }
  941. return 0;
  942. }
  943. static int cell_mqtt_pub(mqtt_conn_t *conn, char *topic, void *data, int len)
  944. {
  945. char buf[200],*pd=(char*)data;
  946. int r=0,rt,offset=0,remain_len,send_len;
  947. sprintf(buf, "=%d,%d,%d,%d,\"%s\",%d", conn->cliId, conn->msgId, conn->qos, conn->retain, topic, len);
  948. rt = send_cmd(conn->h, AT_CMD_QMTPUB, NULL, buf, sizeof(buf));
  949. if(rt!=AT_OK) {
  950. return -1;
  951. }
  952. while(offset<len) {
  953. remain_len = len - offset;
  954. send_len = (remain_len>CHUNCK_SIZE)?CHUNCK_SIZE:remain_len;
  955. rt = send_data(conn->h, data+offset, send_len, 500);
  956. if(rt!=AT_OK) {
  957. r = -1; break;
  958. }
  959. offset += send_len;
  960. }
  961. return 0;
  962. }
  963. static int cell_send_sms(cell_handle_t *h, char *number, char *txt)
  964. {
  965. int r=-1,rt;
  966. char tmp[100],buf[300];
  967. if(h->stat==AT_STAT_UNINIT) {
  968. return -1;
  969. }
  970. // char* unicode_str=utf8_to_unicode_string(txt);
  971. //if (!unicode_str) return -1;
  972. //log_i("unicode_str,%s\n", unicode_str);
  973. log_i("chinese,%s\n", txt);
  974. char ucs2_sms[256];
  975. int ss=chinese_to_ucs2(txt, ucs2_sms,sizeof(ucs2_sms)); // 转换为 UCS2 编码
  976. if (ss!=0) return -1;
  977. log_i("ucs2_sms,%s\n", ucs2_sms);
  978. sprintf(tmp, "=\"%s\"", "UCS2");
  979. r = send_cmd(h, AT_CMD_CSCS, tmp, buf, sizeof(buf));
  980. if (r != AT_OK)
  981. {
  982. return -1;
  983. }
  984. sprintf(tmp, "=17,71,0,8");
  985. r = send_cmd(h, AT_CMD_CSMP, tmp, buf, sizeof(buf));
  986. if (r != AT_OK)
  987. {
  988. return -1;
  989. }
  990. sprintf(tmp, "=\"%s\"", number);
  991. r = send_cmd(h, AT_CMD_CMGS, tmp, buf, sizeof(buf));
  992. if (r != AT_OK)
  993. {
  994. return -1;
  995. }
  996. snprintf(buf, sizeof(buf), "%s\x1a", ucs2_sms);
  997. r = send_data(h, buf, strlen(buf), 5000);
  998. SMS_SEND_COUNT++;
  999. return r;
  1000. }
  1001. int cell_send_sms_all(int type, int subtype, int alarm_type,char *txt)
  1002. {
  1003. cell_handle_t *h=&ceHandle;
  1004. if (!__globalDeviceManage._globalDevInfo.cell.Cellular_enable || __globalDeviceManage._globalDevInfo.cell.mode==0 || SMS_SEND_COUNT >= SMS_SEND_MAX)
  1005. {
  1006. return -1;
  1007. }
  1008. char temp[128];
  1009. char *prod_name = h->dm->_globalDevInfo.product.name;
  1010. int prod_id = h->dm->_globalDevInfo.product.id;
  1011. int langID = h->dm->_globalDevInfo.product.language;
  1012. switch (type)
  1013. {
  1014. case ALARM_TYPE_POWER:
  1015. {
  1016. if (subtype)
  1017. {
  1018. switch (alarm_type)
  1019. {
  1020. case ENABLE_TAC_V_UP:
  1021. case ENABLE_TAC_V_DOWN:
  1022. snprintf(temp, sizeof(temp), "%s %d %s %s %s", prod_name, prod_id, language_alarm_V[langID], language_exceed[langID], language_alarm[langID]);
  1023. break;
  1024. case ENABLE_TAC_A_UP:
  1025. snprintf(temp, sizeof(temp), "%s %d %s %s %s", prod_name, prod_id, language_alarm_A[langID], language_exceed[langID], language_alarm[langID]);
  1026. break;
  1027. case ENABLE_TAC_W_UP:
  1028. snprintf(temp, sizeof(temp), "%s %d %s %s %s", prod_name, prod_id, language_alarm_W[langID], language_exceed[langID], language_alarm[langID]);
  1029. break;
  1030. case ENABLE_TAC_P_UP:
  1031. snprintf(temp, sizeof(temp), "%s %d %s %s %s", prod_name, prod_id, language_alarm_P[langID], language_exceed[langID], language_alarm[langID]);
  1032. break;
  1033. case ENABLE_TAC_STIME:
  1034. case ENABLE_TAC_ETIME:
  1035. snprintf(temp, sizeof(temp), "%s %d %s %s", prod_name, prod_id, language_alarm_phase_lose[langID], language_alarm[langID]);
  1036. break;
  1037. default:
  1038. return -1;
  1039. }
  1040. }
  1041. else
  1042. {
  1043. switch (alarm_type)
  1044. {
  1045. case WARNING_V_UP:
  1046. case WARNING_V_DOWN:
  1047. snprintf(temp, sizeof(temp), "%s %d %s %s %s", prod_name, prod_id, language_alarm_V[langID], language_exceed[langID], language_alarm[langID]);
  1048. break;
  1049. case WARNING_A_UP:
  1050. snprintf(temp, sizeof(temp), "%s %d %s %s %s", prod_name, prod_id, language_alarm_A[langID], language_exceed[langID], language_alarm[langID]);
  1051. break;
  1052. case WARNING_W_UP:
  1053. snprintf(temp, sizeof(temp), "%s %d %s %s %s", prod_name, prod_id, language_alarm_W[langID], language_exceed[langID], language_alarm[langID]);
  1054. break;
  1055. case WARNING_P_UP:
  1056. snprintf(temp, sizeof(temp), "%s %d %s %s %s", prod_name, prod_id, language_alarm_P[langID], language_exceed[langID], language_alarm[langID]);
  1057. break;
  1058. case 0 ... 2:
  1059. snprintf(temp, sizeof(temp), "%s %d %s %s", prod_name, prod_id, language_alarm_phase_lose[langID], language_alarm[langID]);
  1060. break;
  1061. default:
  1062. return -1;
  1063. }
  1064. }
  1065. }
  1066. break;
  1067. case ALARM_TYPE_SENSOR:
  1068. {
  1069. switch (subtype)
  1070. {
  1071. case SENSOR_TYPE_TEMP_DOUBLE:
  1072. case SENSOR_TYPE_TEMP_HUMI:
  1073. case SENSOR_TYPE_TEMP:
  1074. snprintf(temp, sizeof(temp), "%s %d %s/%s %s", prod_name, prod_id, language_temperature[langID], language_humidity[langID], language_alarm[langID]);
  1075. break;
  1076. case SENSOR_TYPE_SMOKE:
  1077. snprintf(temp, sizeof(temp), "%s %d %s %s", prod_name, prod_id, language_sensor_smoke[langID], language_alarm[langID]);
  1078. break;
  1079. case SENSOR_TYPE_WATER:
  1080. snprintf(temp, sizeof(temp), "%s %d %s %s", prod_name, prod_id, language_humidity[langID], language_alarm[langID]);
  1081. break;
  1082. case SENSOR_TYPE_ACCESS:
  1083. snprintf(temp, sizeof(temp), "%s %d %s %s", prod_name, prod_id, language_sensor_access[langID], language_alarm[langID]);
  1084. break;
  1085. case SENSOR_TYPE_GAS:
  1086. snprintf(temp, sizeof(temp), "%s %d %s %s", prod_name, prod_id, language_atmosphere_gas[langID], language_alarm[langID]);
  1087. break;
  1088. case SENSOR_TYPE_AIR_PRESS:
  1089. snprintf(temp, sizeof(temp), "%s %d %s %s", prod_name, prod_id, language_atmosphere_pressure[langID], language_alarm[langID]);
  1090. break;
  1091. case SENSOR_TYPE_LEAK:
  1092. snprintf(temp, sizeof(temp), "%s %d %s %s", prod_name, prod_id, language_sensor_leak[langID], language_alarm[langID]);
  1093. break;
  1094. default:
  1095. return -1;
  1096. }
  1097. }
  1098. break;
  1099. case ALARM_TYPE_NETWORK:
  1100. {
  1101. snprintf(temp, sizeof(temp), "%s %d %s", prod_name, prod_id, language_alarm_Type_Network[langID]);
  1102. }
  1103. break;
  1104. default:
  1105. return -1;
  1106. }
  1107. char result[256];
  1108. snprintf(result, sizeof(result), "%s, %s", temp, txt);
  1109. for (int i = 0; i < MAIL_RECV_MAX; i++)
  1110. {
  1111. if (strcmp(__globalDeviceManage._globalDevInfo.cell.sms_recv[i].account, "") != 0)
  1112. {
  1113. cell_send_sms(h, __globalDeviceManage._globalDevInfo.cell.sms_recv[i].account, result);
  1114. sleep(1);
  1115. }
  1116. }
  1117. return 1;
  1118. }
  1119. static int cell_send_call(cell_handle_t *h, char *number, void *data, int len)
  1120. {
  1121. int r;
  1122. char buf[100];
  1123. if(h->stat==AT_STAT_UNINIT) {
  1124. return -1;
  1125. }
  1126. sprintf(buf, "%s", number);
  1127. r = send_cmd(h, AT_CMD_ATD, NULL, buf, sizeof(buf));
  1128. if(r!=AT_OK) {
  1129. return -1;
  1130. }
  1131. return 0;
  1132. }
  1133. /////////////////////////////////////////////////////////////////////////
  1134. static int conn_cfg(mqtt_conn_t *conn, char *item, char *value)
  1135. {
  1136. int r;
  1137. char tmp[100],buf[200];
  1138. cell_handle_t *h=(cell_handle_t*)conn->h;
  1139. if(h->stat==AT_STAT_UNINIT) {
  1140. return -1;
  1141. }
  1142. sprintf(tmp, "=\"%s\",%s", item, value);
  1143. r = send_cmd(h, AT_CMD_QMTCFG, NULL, buf, sizeof(buf));
  1144. if(r!=AT_OK) {
  1145. return -1;
  1146. }
  1147. return 0;
  1148. }
  1149. static void conn_init(cell_handle_t *h, mqtt_conn_t *conn)
  1150. {
  1151. char buf[100];
  1152. conn->h = h;
  1153. conn->ver = MQTT_VER;
  1154. conn->pdpId = conn->cliId+1; //1~15
  1155. conn->msgId = 0;
  1156. conn->qos = MQTT_QOS;
  1157. conn->retain = 1;
  1158. conn->will.flag = MQTT_WILL;
  1159. conn->will.qos = MQTT_QOS;
  1160. conn->will.retain = MQTT_RETAIN;
  1161. strcpy(conn->will.topic, "");
  1162. strcpy(conn->will.message, "");
  1163. conn->will.message_len = strlen(conn->will.message);
  1164. conn->timeout = 10; //1~60, default:5
  1165. conn->retry_times = 3;
  1166. conn->timeout_notice = 1;
  1167. conn->clean_session = 1;
  1168. conn->ssl_enable = 0;
  1169. conn->ssl_ctx_id = 0;
  1170. conn->msg_recv_mode = 0; //0: urc, 1: not urc
  1171. conn->msg_len_enable = 1;
  1172. conn->ping_interval = 5; //5~60, default:5
  1173. conn->proto_chk_enable = 0; //0:no check 1:check
  1174. conn->send_mode = 0; //0:string 1:hex
  1175. conn->recv_mode = 0; //0:string 1:hex
  1176. conn->view_mode = 0; //0:no echo 1:echo
  1177. conn->edit_mode = 0; //0:timeout not quit 1:timeout quit
  1178. conn->edit_time = 5; //1~120 seconds
  1179. //conn->plat.product_key[64]; //aliyun used
  1180. //conn->plat.device_name[64]; //aliyun used
  1181. //conn->plat.device_secret[64]; //aliyun/ used
  1182. //conn->plat.hw_time_enable; //huawei used
  1183. //conn->plat.product_secret[64]; //huawei used
  1184. //conn->plat.node_id[64];
  1185. //conn->plat.product_id[64]; //onenet used
  1186. //conn->plat.access_key[64]; //onenet used
  1187. sprintf(buf, "%d,%d", conn->cliId, conn->ver);
  1188. conn_cfg(conn, "version", buf);
  1189. sprintf(buf, "%d,%d", conn->cliId, conn->pdpId);
  1190. conn_cfg(conn, "pdpcid", buf);
  1191. sprintf(buf, "%d,%d", conn->cliId, conn->keep_alive_time);
  1192. conn_cfg(conn, "keepalive", buf);
  1193. sprintf(buf, "%d,%d", conn->cliId, conn->clean_session);
  1194. conn_cfg(conn, "session", buf);
  1195. sprintf(buf, "%d,%d,%d,%d", conn->cliId, conn->timeout, conn->retry_times, conn->timeout_notice);
  1196. conn_cfg(conn, "timeout", buf);
  1197. sprintf(buf, "%d,%d,%d", conn->cliId, conn->ssl_enable, conn->ssl_ctx_id);
  1198. conn_cfg(conn, "ssl", buf);
  1199. sprintf(buf, "%d,%d", conn->cliId, conn->ping_interval);
  1200. conn_cfg(conn, "qmtping", buf);
  1201. //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);
  1202. //conn_cfg(conn, "will", buf);
  1203. //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);
  1204. //conn_cfg(conn, "willex", buf);
  1205. sprintf(buf, "%d,%d", conn->cliId, conn->send_mode);
  1206. conn_cfg(conn, "send/mode", buf);
  1207. sprintf(buf, "%d,%d,%d", conn->cliId, conn->msg_recv_mode, conn->msg_len_enable);
  1208. conn_cfg(conn, "recv/mode", buf);
  1209. sprintf(buf, "%d,%d", conn->cliId, conn->view_mode);
  1210. conn_cfg(conn, "view/mode", buf);
  1211. sprintf(buf, "%d,%d,%d", conn->cliId, conn->edit_mode, conn->edit_time);
  1212. conn_cfg(conn, "edit/timeout", buf);
  1213. sprintf(buf, "%d,%d", conn->cliId, conn->proto_chk_enable);
  1214. conn_cfg(conn, "protocol/check", buf);
  1215. sprintf(buf, "%d,%d,%d", conn->cliId, conn->send_mode, conn->recv_mode);
  1216. conn_cfg(conn, "dataformat", buf);
  1217. #if 0 //aliyun
  1218. sprintf(buf, "%d,\"%s\",\"%s\",\"%s\"", conn->cliId, conn->plat.product_key, conn->plat.device_name, conn->plat.device_secret);
  1219. conn_cfg(conn, "aliauth", buf);
  1220. #endif
  1221. #if 0 //huawei
  1222. sprintf(buf, "%d,\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.device_secret);
  1223. conn_cfg(conn, "hwauth", buf);
  1224. sprintf(buf, "%d,\"%s\",\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.product_secret, conn->plat.node_id);
  1225. conn_cfg(conn, "hwprodid", buf);
  1226. #endif
  1227. #if 0 //onenet
  1228. sprintf(buf, "%d,\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.access_key);
  1229. conn_cfg(conn, "onenet", buf);
  1230. #endif
  1231. }
  1232. static int cell_test(cell_handle_t *h)
  1233. {
  1234. int r;
  1235. cell_info_t info;
  1236. cell_stat_t stat;
  1237. cell_get_info(h, &info);
  1238. while(1) {
  1239. r = cell_get_stat(h, &stat);
  1240. if(r==0) {
  1241. LOGD("___dbm: %d ber: %d\n", stat.dbm, stat.ber);
  1242. }
  1243. else {
  1244. LOGE("___ get stat failed\n");
  1245. }
  1246. sleep(3);
  1247. }
  1248. return 0;
  1249. }
  1250. /////////////////////////////////////////////////////////////////////////
  1251. static int urc_handle(char *data, int len)
  1252. {
  1253. char *key=NULL;
  1254. key = "+QIURC: ";
  1255. if(strstr(data, key)) {
  1256. }
  1257. key = "+QMT";
  1258. if(strstr(data, key)) {
  1259. }
  1260. return 0;
  1261. }
  1262. static void* cellular_thread(void *arg)
  1263. {
  1264. int r;
  1265. uint32_t cnt=0,prelinkcnt=0;
  1266. char rxbuf[BUF_LEN];
  1267. thread_handle_t *th=(thread_handle_t*)arg;
  1268. cell_handle_t *h=(cell_handle_t*)th->arg;
  1269. port_init(h);
  1270. while (th->quit==0) {
  1271. cnt++;
  1272. if(h->pcell->mode) {
  1273. if (h->fd<0)
  1274. {
  1275. port_init(h);
  1276. }
  1277. if(cnt%5==0) {
  1278. cell_init(h);
  1279. }
  1280. if (cnt % 5 == 0)
  1281. {
  1282. // cell_get_info(h, &h->all.info);
  1283. cell_get_stat(h, &h->all.stat);
  1284. }
  1285. if (h->all.stat.linked == 1)
  1286. {
  1287. prelinkcnt = cnt;
  1288. }
  1289. if (cnt -prelinkcnt >=60 && h->all.stat.linked == 0)
  1290. {
  1291. prelinkcnt = cnt;
  1292. cell_reset(h);
  1293. }
  1294. }
  1295. else
  1296. {
  1297. if( h->fd>=0)
  1298. {
  1299. cell_deinit(h);
  1300. port_deinit(h);
  1301. }
  1302. reset_var(h);
  1303. }
  1304. UpdateCellularManager(&h->all);
  1305. sleep(1);
  1306. }
  1307. cell_deinit(h);
  1308. port_deinit(h);
  1309. pthread_exit(NULL);
  1310. }
  1311. int cellular_reset(void)
  1312. {
  1313. int r=-1;
  1314. cell_handle_t *h=&ceHandle;
  1315. r = cell_reset(h);
  1316. return r;
  1317. }
  1318. int cellular_init(void)
  1319. {
  1320. int r;
  1321. cell_handle_t *h=&ceHandle;
  1322. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  1323. {
  1324. memset(h, 0, sizeof(cell_handle_t));
  1325. h->dm = &__globalDeviceManage;
  1326. h->pcell = &h->dm->_globalDevInfo.cell;
  1327. h->prod_id = h->dm->_globalDevInfo.product.id;
  1328. h->lck = lock_d_init();
  1329. nic_set(NIC_USB, 1);
  1330. dev_SMS_get(h->dm->db, h->pcell);
  1331. thread_start(THREAD_ID_CELLULAR, cellular_thread, h);
  1332. }
  1333. return 0;
  1334. }
  1335. int cellular_deinit(void)
  1336. {
  1337. cell_handle_t *h=&ceHandle;
  1338. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  1339. {
  1340. lock_d_deinit(h->lck);
  1341. thread_stop(THREAD_ID_CELLULAR);
  1342. }
  1343. return 0;
  1344. }
  1345. int cellular_get_all(cell_all_t *all)
  1346. {
  1347. int r=-1;
  1348. cell_handle_t *h=&ceHandle;
  1349. if(all) {
  1350. *all = h->all;
  1351. r = 0;
  1352. }
  1353. return r;
  1354. }
  1355. int cellular_is_connected(void)
  1356. {
  1357. int r=0;
  1358. cell_handle_t *h=&ceHandle;
  1359. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  1360. {
  1361. r = h->all.stat.linked;
  1362. }
  1363. return r;
  1364. }
  1365. int UpdateCellularManager( cell_all_t *all)
  1366. {
  1367. int r = -1;
  1368. if (all)
  1369. {
  1370. __globalDeviceManage._globalDevInfo.cell.Link_status = all->stat.linked;
  1371. __globalDeviceManage._globalDevInfo.cell.Signal_strength = all->stat.dbm;
  1372. strcpy( __globalDeviceManage._globalDevInfo.cell.apn , all->info.apn);
  1373. strcpy( __globalDeviceManage._globalDevInfo.cell.Cellular_name , all->info.isp);
  1374. strcpy( __globalDeviceManage._globalDevInfo.cell.Cellular_Num , all->info.imei);
  1375. r=0;
  1376. }
  1377. return r;
  1378. }
  1379. int cellular_test(void)
  1380. {
  1381. cell_handle_t *h=&ceHandle;
  1382. cell_test(h);
  1383. return 0;
  1384. }