message.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. #include "includes.h"
  2. SUT_SMS_DEFINE sutSms;
  3. SUT_MESSAGE_EDIT sutMEdit;
  4. static void SmsScan(unsigned char ret);
  5. void ShowMessageFlag(unsigned char show){
  6. static unsigned char smsUnReadBackup;
  7. unsigned char showFlag;
  8. showFlag=0;
  9. if(show){
  10. if(sutSms.haveUnRead) showFlag=1;
  11. }else{
  12. if(smsUnReadBackup == sutSms.haveUnRead) return;
  13. smsUnReadBackup = sutSms.haveUnRead;
  14. if(sutSms.haveUnRead) showFlag=1;
  15. }
  16. switch(showFlag)
  17. {
  18. case 0:
  19. guiClearRect(SMS_ICON_X,0,SMS_ICON_X+SMS_ICON_WX,12,COLOR_STATUS_BAR);
  20. //guiShowStr(70,1," ",FONT_MODE_12X12, REVERSED_NO, guiGetForeColor(), COLOR_STATUS_BAR);
  21. break;
  22. case 1:guiShowBmp(SMS_ICON_X,3,"Mail1.bmp");break;
  23. //case 2:GuiShowStr(60,3," ",0x01,0);break;
  24. default:break;
  25. }
  26. }
  27. //读取所有信息的前15字节内容
  28. //返回消息总数目
  29. unsigned char GetPagePreMessage(SUT_LIST_BOX *p,unsigned char firstRead, char up_down){
  30. unsigned char i,*ptr,buf[16],ptrTem[8];
  31. uint32_t readAddr;
  32. uint16_t totalnum,retNum;
  33. SUT_SMS_INFO_HEADER info;
  34. unsigned char downFindFirst;//向下查找时第一次找到消息-此时要记录向上查找记录upToBeRead
  35. unsigned char upFindFirst;
  36. unsigned char reCtPerTime;
  37. int fd;
  38. //以下用于交换用
  39. SUT_LIST_BOX_SMS_INFO boxinfo;
  40. unsigned char temp,*Cptr1,*Cptr2;
  41. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDONLY);
  42. if(fd<0){
  43. MSG_ERR(1,"%s GetPagePreMessage open failed", SMS_FILE);
  44. return;
  45. }
  46. //1从最新消息,即sutSms.newestAddr开始读出最多5项
  47. if(sutSms.smsTotalNum >= LIST_ROW) reCtPerTime=LIST_ROW;
  48. else reCtPerTime = sutSms.smsTotalNum;
  49. downFindFirst=0;
  50. upFindFirst=0;
  51. totalnum = SMS_TOTAL_NUM;
  52. ptr=(unsigned char *)&info;
  53. if(firstRead == 1){
  54. //SmsScan(1);
  55. sutSms.downToBeRead = sutSms.newestAddr;
  56. nwy_sdk_fseek(fd, sutSms.newestAddr, NWY_SEEK_SET);
  57. nwy_sdk_fread(fd, ptr,sizeof( SUT_SMS_INFO_HEADER));
  58. //sFlash_Read(ptr, sutSms.newestAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  59. sutSms.upToBeRead = info.upperAddr;
  60. firstRead=0;
  61. }
  62. i=0;
  63. if(up_down == 'd')
  64. {//向下读
  65. while(1)
  66. {
  67. readAddr = sutSms.downToBeRead;
  68. //1.1读出年月日(16/06/01 = 8字节)+内容7字节
  69. nwy_sdk_fseek(fd, readAddr, NWY_SEEK_SET);
  70. nwy_sdk_fread(fd, ptr,sizeof( SUT_SMS_INFO_HEADER));
  71. nwy_sdk_fseek(fd, readAddr+SMS_INFO_DETAIL_OFFSET, NWY_SEEK_SET);
  72. nwy_sdk_fread(fd, ptrTem,8);
  73. //sFlash_Read(ptr, readAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  74. //sFlash_Read(ptrTem, readAddr+SMS_INFO_DETAIL_OFFSET, 8);//8字节内容
  75. if(1==info.flag)
  76. {
  77. if(!downFindFirst)
  78. {
  79. downFindFirst++;
  80. sutSms.upToBeRead = info.upperAddr;
  81. }
  82. sprintf(p->boxinfo[i].item,"%02d/%02d/%02d ", (info.recTime>>26)&0x3F,(info.recTime>>22)&0x0F,(info.recTime>>17)&0x1F);
  83. strcat(p->boxinfo[i].item, (char *)ptrTem);
  84. if(1==info.read)
  85. p->boxinfo[i].features = 0;
  86. else p->boxinfo[i].features = 1;
  87. sutSms.downToBeRead = info.nextAddr;
  88. sutSms.ReadAddr[i] = readAddr;
  89. i++;
  90. if(i>=reCtPerTime)
  91. {
  92. retNum=i;
  93. break;
  94. }
  95. }else
  96. {//读完了,转到一开始
  97. sutSms.downToBeRead = sutSms.newestAddr;
  98. //sutSms.downToBeRead = info.nextAddr;
  99. continue;
  100. }
  101. totalnum--;
  102. if(!totalnum)
  103. {
  104. //error
  105. MSG_ERR(1, "[SMS]err1");
  106. retNum=i;
  107. break;
  108. }
  109. }
  110. p->handle=0;
  111. }else
  112. {//向上读
  113. while(1)
  114. {
  115. readAddr = sutSms.upToBeRead;
  116. //1.1读出年月日(16/06/01 = 8字节)+内容7字节
  117. nwy_sdk_fseek(fd, readAddr, NWY_SEEK_SET);
  118. nwy_sdk_fread(fd, ptr,sizeof( SUT_SMS_INFO_HEADER));
  119. nwy_sdk_fseek(fd, readAddr+SMS_INFO_DETAIL_OFFSET, NWY_SEEK_SET);
  120. nwy_sdk_fread(fd, ptrTem,8);
  121. //sFlash_Read(ptr, readAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  122. //sFlash_Read(ptrTem, readAddr+SMS_INFO_DETAIL_OFFSET, 8);//8字节内容
  123. if(1==info.flag)
  124. {
  125. if(!upFindFirst)
  126. {
  127. upFindFirst++;
  128. sutSms.downToBeRead = info.nextAddr;
  129. }
  130. sprintf(p->boxinfo[i].item,"%d/%d/%d ", (info.recTime>>26)&0x3F,(info.recTime>>22)&0x0F,(info.recTime>>17)&0x1F);
  131. strcat(p->boxinfo[i].item, (char *)ptrTem);
  132. if(1==info.read)
  133. p->boxinfo[i].features = 0;
  134. else p->boxinfo[i].features = 1;
  135. sutSms.upToBeRead = info.upperAddr;
  136. sutSms.ReadAddr[i] = readAddr;
  137. i++;
  138. if(i>=reCtPerTime)
  139. {
  140. retNum=i;
  141. break;
  142. }
  143. }else
  144. {//读完了,转到最后一条
  145. //sutSms.upToBeRead = sutSms.newestAddr+(sutSms.smsTotalNum-1)*SMS_SIZE_PER;
  146. //sutSms.upToBeRead = SMS_START_ADDRESS+(sutSms.smsTotalNum-1)*SMS_SIZE_PER;
  147. sutSms.upToBeRead = info.upperAddr;
  148. continue;
  149. }
  150. totalnum--;
  151. if(!totalnum)
  152. {
  153. //error
  154. MSG_ERR(1,"[SMS]err2");
  155. retNum=i;
  156. break;
  157. }
  158. }
  159. //switch
  160. ptr=(unsigned char *)&boxinfo;//copy
  161. temp = retNum/2;
  162. for(i=0;i<temp;i++)
  163. {
  164. Cptr1=(unsigned char *)&p->boxinfo[i];
  165. Cptr2=(unsigned char *)&p->boxinfo[retNum-1-i];
  166. memcpy(ptr, Cptr1, LIST_ITEM_TEXT_LEN_MAX+1);
  167. memcpy(Cptr1, Cptr2, LIST_ITEM_TEXT_LEN_MAX+1);
  168. memcpy(Cptr2, ptr, LIST_ITEM_TEXT_LEN_MAX+1);
  169. readAddr=sutSms.ReadAddr[i];
  170. sutSms.ReadAddr[i] = sutSms.ReadAddr[retNum-1-i];
  171. sutSms.ReadAddr[retNum-1-i] = readAddr;
  172. }
  173. p->handle=reCtPerTime-1;
  174. }
  175. if(fd>0) nwy_sdk_fclose(fd);
  176. p->itemnum=retNum;
  177. p->pageChange=0;
  178. ListBoxItemNumShowSMS(p->notehandle,p->totalnum);
  179. ListBoxShowSMS(p);
  180. }
  181. void SetGotNewMessage(void)
  182. {
  183. unsigned char type,encode,def1,def2;
  184. type = sutMess.GBKMess1[0];
  185. encode=sutMess.GBKMess1[1];
  186. def1 = sutMess.GBKMess1[2];
  187. def2 = sutMess.GBKMess1[3];
  188. //处理分析消息类型
  189. if(type == 0x30 &&
  190. encode == 0x30 &&
  191. def1 == 0x32 &&
  192. def2 == 0x30)
  193. {//一键报警
  194. sutMess.len -= 4;
  195. sutMess.Update=1;//收到一条消息标记
  196. sutMess.startCntFlag=1;
  197. sutMess.skipCnt=0;//从此时开始计数
  198. sutMess.forcePullGroupOn=0;
  199. MSG_WARN(1,"AlarmStart");
  200. sutMess.codeType=2;//unicode
  201. //sutSms.smsUpdate=1;//need to store
  202. }else
  203. if(type == 0x01 &&
  204. encode == 0x02)
  205. {//纯TTS播报
  206. sutMess.len -= 4;
  207. sutMess.Update=2;
  208. sutMess.codeType=2;//unicode
  209. sutSms.smsUpdate=1;//need to store
  210. MSG_WARN(1,"Pure TTS");
  211. }else
  212. if(type == 0x20 &&
  213. encode == 0x20 &&
  214. def1 == 0x20 &&
  215. def2 == 0x20)
  216. {//全文本
  217. sutMess.len -= 4;
  218. sutMess.Update=0;
  219. sutMess.codeType=1;//gb2312
  220. sutSms.smsUpdate=1;//need to store
  221. MSG_WARN(1,"Pure TXT");
  222. }
  223. // sutFeed.result=0;
  224. // sutFeed.feedBackStart=1;
  225. }
  226. //扫描消息 ret值:
  227. //0、重置过的
  228. //1、获得消息总数以及未读数目
  229. //2、往上查找当前最新消息位置上一条空地址
  230. static void SmsScan(unsigned char ret)
  231. {
  232. unsigned char *ptr;
  233. unsigned short i;
  234. SUT_SMS_INFO_HEADER smsinfo,*info;
  235. unsigned int readAddr;
  236. int fd=-1;
  237. if(ret > 2) return;//出错不扫描
  238. if(0!=ret){
  239. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDONLY);
  240. if(fd<0){
  241. MSG_ERR(1,"%s smsscan but open failed", SMS_FILE);
  242. return;
  243. }
  244. }
  245. if(0 == ret)
  246. {//已reset过
  247. }else if(1 == ret)
  248. {//查找消息总数和未读数
  249. sutSms.smsTotalNum=0;
  250. sutSms.smsUnReadNum=0;
  251. ptr = (unsigned char *)&smsinfo;
  252. readAddr = sutSms.newestAddr;
  253. info=&smsinfo;
  254. i=SMS_TOTAL_NUM;
  255. while(1)
  256. {
  257. nwy_sdk_fseek(fd, readAddr, NWY_SEEK_SET);
  258. nwy_sdk_fread(fd, ptr, sizeof(SUT_SMS_INFO_HEADER));
  259. //sFlash_Read(ptr, readAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  260. if(1==smsinfo.flag)
  261. {
  262. sutSms.smsTotalNum ++;
  263. if(1==smsinfo.read)
  264. {
  265. sutSms.smsUnReadNum ++;
  266. sutSms.haveUnRead = 1;
  267. }
  268. }
  269. i--;
  270. if(!i) break;
  271. readAddr = info->nextAddr;
  272. }
  273. }else if(2 == ret)
  274. {//向上查找空地址
  275. ptr = (unsigned char *)&smsinfo;
  276. readAddr = sutSms.newestAddr;
  277. info=&smsinfo;
  278. i=SMS_TOTAL_NUM;
  279. while(1)
  280. {
  281. nwy_sdk_fseek(fd, readAddr, NWY_SEEK_SET);
  282. nwy_sdk_fread(fd, ptr, sizeof(SUT_SMS_INFO_HEADER));
  283. //sFlash_Read(ptr, readAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  284. if(0==smsinfo.flag)
  285. {//找到
  286. sutSms.emptyAddr = readAddr;
  287. break;
  288. }
  289. i--;
  290. if(!i)
  291. {
  292. sutSms.poolFull = 1;
  293. MSG_WARN(1,"[SMS]Full");
  294. //SpeakerEnable();
  295. ttsPlay(ENCODE_USER_GBK,"CFFBCFA2D2D1C2FA");
  296. break;
  297. }
  298. readAddr = smsinfo.upperAddr;
  299. }
  300. }
  301. if(fd>0) nwy_sdk_fclose(fd);
  302. }
  303. ///////////////////////////////////////////////////////////
  304. //uint8_t defaultSMS[] = "0我们都是好朋友";
  305. //重置设置
  306. //返回值0到2用实际用处
  307. unsigned char ResetSMSConfi(void){
  308. int endAddress,temp1,i;
  309. unsigned char *ptr,process=0;
  310. SUT_SMS_INFO_HEADER smsinfo;
  311. unsigned short sectorNum,startSector,k,x,y,lx;
  312. unsigned int totalBytes;
  313. unsigned char tbuf[SMS_SIZE_PER];
  314. long ret;
  315. switch(newPara.messageResetFlag){
  316. case 'N'://不用重置
  317. sutSms.smsEnable = 1;
  318. //读出最新消息地址
  319. sutSms.newestAddr = newPara.messageNewAddr;
  320. MSG_WARN(1,"[SMS]IDLE");
  321. return 1;
  322. break;
  323. case 'R'://重置
  324. MSG_WARN(1,"[SMS]Reset");
  325. break;
  326. default://文件内容错误
  327. MSG_ERR(1,"[SMS]RErr3");
  328. sutSms.smsEnable = 0;
  329. return 5;
  330. break;
  331. }
  332. //2,根据短信格式初始化消息内存
  333. y=(GLCD_HEIGHT-24-4)/2;
  334. guiShowStr(26,y,"消息内存配置",FONT_MODE_12X12, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  335. x=29;y+=(12+4);lx=x;
  336. guiDrawRect(x,y,x+100,y+16,1,COLOR_BLACK);
  337. //guiDrawRect(29,50+50,128,66+50,1,COLOR_BLACK);
  338. //新建文件,如果存在则清空
  339. int fd;
  340. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR|NWY_CREAT);
  341. if(fd<0){
  342. MSG_ERR(1,"[SMS]%s open err",SMS_FILE);
  343. sutSms.smsEnable = 0;
  344. return 5;
  345. }
  346. ptr = (unsigned char *)&smsinfo;
  347. for(i=0;i<SMS_TOTAL_NUM;i++){
  348. smsinfo.flag = 0;
  349. smsinfo.read = 0;
  350. smsinfo.len = 0;
  351. smsinfo.recTime = 0;
  352. if(0==i)
  353. {//第一条
  354. smsinfo.upperAddr = SMS_START_ADDRESS+(SMS_TOTAL_NUM-1)*SMS_SIZE_PER;//连接最后一条地址
  355. smsinfo.nextAddr = SMS_START_ADDRESS+SMS_SIZE_PER;//指向下一条地址
  356. sutSms.emptyAddr = smsinfo.upperAddr;//空地址为上一条
  357. //sFlash_Write(defaultSMS, SMS_START_ADDRESS+SMS_INFO_DETAIL_OFFSET, sizeof(defaultSMS));
  358. }else if(i == (SMS_TOTAL_NUM-1))
  359. {//最后一条
  360. smsinfo.upperAddr = SMS_START_ADDRESS+(SMS_TOTAL_NUM-2)*SMS_SIZE_PER;//倒数第二条地址
  361. smsinfo.nextAddr = SMS_START_ADDRESS;//第一条地址
  362. //sFlash_Write(defaultSMS, SMS_START_ADDRESS+i*SMS_SIZE_PER+SMS_INFO_DETAIL_OFFSET, sizeof(defaultSMS));
  363. }
  364. else
  365. {
  366. smsinfo.upperAddr = SMS_START_ADDRESS+(i-1)*SMS_SIZE_PER;
  367. smsinfo.nextAddr = SMS_START_ADDRESS+(i+1)*SMS_SIZE_PER;
  368. //sFlash_Write(defaultSMS, SMS_START_ADDRESS+i*SMS_SIZE_PER+SMS_INFO_DETAIL_OFFSET, sizeof(defaultSMS));
  369. }
  370. nwy_sdk_fseek(fd, SMS_START_ADDRESS+i*SMS_SIZE_PER, NWY_SEEK_SET);
  371. //nwy_sdk_fwrite(fd,ptr,sizeof(SUT_SMS_INFO_HEADER));
  372. memset(tbuf, 0, sizeof(tbuf));
  373. memcpy(tbuf, ptr, sizeof(SUT_SMS_INFO_HEADER));
  374. ret=nwy_sdk_fwrite(fd,tbuf,SMS_SIZE_PER);
  375. if(ret==-1) MSG_INFO(1, "sms init failed:%d",i);
  376. if(i%10==0){
  377. process = i*100/SMS_TOTAL_NUM;
  378. guiFillRect(lx,y+2,x+process,y+14,COLOR_BLACK);
  379. lx=x+process;
  380. }
  381. }
  382. guiFillRect(lx,y+2,x+100,y+14,COLOR_BLACK);
  383. nwy_sdk_fclose(fd);
  384. newPara.messageResetFlag = 'N';
  385. newPara.messageNewAddr = SMS_START_ADDRESS;
  386. //save sys
  387. sysIniSave();
  388. //3,初始化参数
  389. sutSms.newestAddr = SMS_START_ADDRESS;
  390. sutSms.smsTotalNum=0;
  391. sutSms.smsUnReadNum=0;
  392. sutSms.haveUnRead = 0;
  393. sutSms.smsUpdate = 0;
  394. sutSms.poolFull = 0;
  395. sutSms.smsEnable = 1;
  396. SmsScan(1);
  397. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDONLY);
  398. if(fd<0) MSG_WARN(1,"%s open to read but failed",SMS_FILE);
  399. else{
  400. nwy_sdk_fseek(fd, SMS_START_ADDRESS, NWY_SEEK_SET);
  401. nwy_sdk_fread(fd, ptr, sizeof( SUT_SMS_INFO_HEADER));
  402. nwy_sdk_fclose(fd);
  403. }
  404. return 0;
  405. }
  406. void SetMessageConfi(void)
  407. {
  408. SmsScan(ResetSMSConfi());
  409. MSG_INFO(1,"[SMS]Ok");
  410. }
  411. void MessageResponse(void)
  412. {
  413. if(sutSms.smsTotalNum) ListBoxResponseSMS(&sutListBox);
  414. switch(getKeyValue()){
  415. case MKEY_VALUE_ESC:
  416. uISetNextStatus(UIS_MENU_MAIN);
  417. break;
  418. case MKEY_VALUE_MENU:
  419. if(sutSms.smsTotalNum)
  420. uISetNextStatus(UIS_MENU_MAIL_READ);
  421. break;
  422. case MKEY_VALUE_P1:
  423. uISetNextStatus(UIS_MENU_MAIL_OPTION);
  424. break;
  425. default:
  426. break;
  427. }
  428. }
  429. void MessageDeletShow(char update)
  430. {
  431. if(update)
  432. {
  433. guiClearAll(guiGetBackColor());//clear
  434. guiShowCaption(0,"消息删除",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  435. guiShowMessageBox("确定删除?");
  436. uiMenuShowBottomLine();
  437. uiMenuShowBottomIndacitor("确定", "取消");
  438. }
  439. }
  440. void MessageFormatShow(char update)
  441. {
  442. if(update)
  443. {
  444. guiClearAll(guiGetBackColor());//clear
  445. guiShowCaption(0,"消息格式化",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  446. guiShowMessageBox("确定删除所有?");
  447. uiMenuShowBottomLine();
  448. uiMenuShowBottomIndacitor("确定", "取消");
  449. }
  450. }
  451. const char *accIconFileName[]={
  452. "Mail1.bmp",
  453. "Mail2.bmp",
  454. ""
  455. };
  456. /******************************************************
  457. uiShowMailBox
  458. 信箱
  459. *******************************************************/
  460. void uiShowMailBox(char update){
  461. if(update){
  462. guiClearAll(guiGetBackColor());
  463. guiShowCaption(40,"文本消息",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  464. uiMenuShowBottomLine();
  465. #if 0
  466. if(sutSms.smsTotalNum>=1){
  467. ListBoxInitSMS(&sutListBox,sutSms.smsTotalNum,accIconFileName,0);
  468. uiMenuShowBottomIndacitor("查阅", "[P1]选项");
  469. }else{
  470. ListBoxItemNumShowSMS(0,0);
  471. uiMenuShowBottomIndacitor(NULL, "[P1]选项");
  472. }
  473. #else
  474. ListBoxItemNumShowSMS(0,0);
  475. uiMenuShowBottomIndacitor(NULL, "[P1]选项");
  476. guiShowMessageBox("不支持");
  477. nwy_sleep(500);
  478. uISetNextStatus(UIS_MENU_MAIL_SEL);
  479. #endif
  480. }
  481. }
  482. void DeleteSpecificSMS(void)
  483. {
  484. SUT_SMS_INFO_HEADER smsinfo,smsinfoTem,*info;
  485. unsigned char *ptr,*ptrTem,temp;
  486. unsigned int curAddr;
  487. int fd;
  488. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR);
  489. if(fd<0){
  490. MSG_ERR(1,"%s del open but failed", SMS_FILE);
  491. return;
  492. }
  493. ptr=(unsigned char *)&smsinfo;
  494. info=&smsinfo;
  495. //curAddr=SMS_START_ADDRESS+index*SMS_SIZE_PER;//要删除的消息地址
  496. curAddr=sutSms.ReadAddr[sutListBox.handle];
  497. nwy_sdk_fseek(fd, curAddr,NWY_SEEK_SET);
  498. nwy_sdk_fread(fd, ptr, sizeof( SUT_SMS_INFO_HEADER));
  499. //sFlash_Read(ptr, curAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  500. //0 更新数目
  501. if(1==smsinfo.read)
  502. {
  503. if(sutSms.smsUnReadNum)
  504. {
  505. sutSms.smsUnReadNum --;
  506. if(0==sutSms.smsUnReadNum)
  507. sutSms.haveUnRead =0;
  508. }
  509. }
  510. if(sutSms.smsTotalNum)
  511. sutSms.smsTotalNum --;
  512. if(sutSms.poolFull)
  513. sutSms.poolFull--;
  514. if(curAddr != sutSms.newestAddr){
  515. //1将当前要删除的消息的上下消息接连起来
  516. //1.1设置上一条消息的nextAddr
  517. ptrTem=(unsigned char *)&smsinfo.nextAddr;
  518. nwy_sdk_fseek(fd,smsinfo.upperAddr+SMS_INFO_REAL_LEN+4,NWY_SEEK_SET );
  519. nwy_sdk_fwrite(fd, ptrTem, 4);
  520. //sFlash_Write(ptrTem, smsinfo.upperAddr+SMS_INFO_REAL_LEN+4, 4);
  521. //1.2设置下一条消息的upperAddr
  522. ptrTem=(unsigned char *)&smsinfo.upperAddr;
  523. nwy_sdk_fseek(fd,smsinfo.nextAddr+SMS_INFO_REAL_LEN,NWY_SEEK_SET );
  524. nwy_sdk_fwrite(fd, ptrTem, 4);
  525. //sFlash_Write(ptrTem, smsinfo.nextAddr+SMS_INFO_REAL_LEN, 4);
  526. }
  527. //2设置要删除的消息的标志位
  528. temp=0;
  529. nwy_sdk_fseek(fd,curAddr,NWY_SEEK_SET );
  530. nwy_sdk_fwrite(fd, &temp, 1);
  531. //sFlash_Write(&temp, curAddr, 1);
  532. if(curAddr != sutSms.newestAddr)
  533. {
  534. //3把空的消息插到最新的那条消息的上面去,它的下面都会是连续的新消息,不要往下插
  535. nwy_sdk_fseek(fd, sutSms.newestAddr, NWY_SEEK_SET);
  536. nwy_sdk_fread(fd, ptr, sizeof( SUT_SMS_INFO_HEADER));
  537. //sFlash_Read(ptr, sutSms.newestAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  538. //3.1设置最新的消息的upperAddr
  539. nwy_sdk_fseek(fd, sutSms.newestAddr+SMS_INFO_REAL_LEN, NWY_SEEK_SET);
  540. nwy_sdk_fwrite(fd, (unsigned char *)&curAddr, 4);
  541. //sFlash_Write((uint8_t *)&curAddr, sutSms.newestAddr+SMS_INFO_REAL_LEN, 4);
  542. //3.2设置最新消息原来的上一条消息的nextAddr
  543. nwy_sdk_fseek(fd, smsinfo.upperAddr+SMS_INFO_REAL_LEN+4, NWY_SEEK_SET);
  544. nwy_sdk_fwrite(fd, (unsigned char *)&curAddr, 4);
  545. //sFlash_Write((uint8_t *)&curAddr, smsinfo.upperAddr+SMS_INFO_REAL_LEN+4, 4);
  546. //4设置被删除的消息的upperAddr and nextAddr
  547. //4.1设置upperAddr
  548. nwy_sdk_fseek(fd, curAddr+SMS_INFO_REAL_LEN, NWY_SEEK_SET);
  549. nwy_sdk_fwrite(fd, (unsigned char *)&smsinfo.upperAddr, 4);
  550. //sFlash_Write((uint8_t *)&smsinfo.upperAddr, curAddr+SMS_INFO_REAL_LEN, 4);
  551. //4.2设置nextAddr
  552. nwy_sdk_fseek(fd, curAddr+SMS_INFO_REAL_LEN+4, NWY_SEEK_SET);
  553. nwy_sdk_fwrite(fd, (unsigned char *)&sutSms.newestAddr, 4);
  554. //sFlash_Write((uint8_t *)&sutSms.newestAddr, curAddr+SMS_INFO_REAL_LEN+4, 4);
  555. }else
  556. {
  557. //如果是删除最新的那条消息,不用更改位置,只要将更改最新消息的地址即可
  558. sutSms.newestAddr = smsinfo.nextAddr;
  559. }
  560. if(fd>0) nwy_sdk_fclose(fd);
  561. }
  562. void MessageDeletResponse(void)
  563. {
  564. unsigned short key=getKeyValue();
  565. if(MKEY_VALUE_MENU==key)
  566. {
  567. if(sutSms.smsTotalNum>=1)
  568. DeleteSpecificSMS();
  569. uISetNextStatus(UIS_MENU_MAIL_SEL);
  570. }
  571. if(MKEY_VALUE_ESC==key)uISetNextStatus(UIS_MENU_MAIL_SEL);
  572. }
  573. void FormatSMS(void)
  574. {
  575. unsigned short i;
  576. unsigned char *ptr;
  577. SUT_SMS_INFO_HEADER info;
  578. unsigned int readAddr;
  579. unsigned char process;
  580. int fd;
  581. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR);
  582. if(fd<0){
  583. MSG_ERR(1,"%s format but open failed", SMS_FILE);
  584. return;
  585. }
  586. guiClearRect(0,44,GLCD_WIDTH,84,guiGetBackColor());
  587. guiDrawRect(29,50,128,66,1,guiGetForeColor());
  588. process=0;
  589. readAddr = sutSms.newestAddr;
  590. ptr=(unsigned char *)&info;
  591. for(i=0;i<SMS_TOTAL_NUM;i++)
  592. {
  593. nwy_sdk_fseek(fd, readAddr,NWY_SEEK_SET);
  594. nwy_sdk_fread(fd, ptr, sizeof(SUT_SMS_INFO_HEADER));
  595. //sFlash_Read(ptr, readAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  596. if(info.flag == 1)
  597. {
  598. info.flag=0;
  599. nwy_sdk_fseek(fd, readAddr,NWY_SEEK_SET);
  600. nwy_sdk_fwrite(fd, ptr, 1);
  601. //sFlash_Write(ptr, readAddr, 1);
  602. }
  603. readAddr = info.nextAddr;
  604. if(i%10==0)
  605. {
  606. guiDrawVLine(52,64,29+process,1,guiGetForeColor());
  607. process++;
  608. }
  609. }
  610. if(fd>0) nwy_sdk_fclose(fd);
  611. sutSms.smsTotalNum = 0;
  612. sutSms.smsUnReadNum = 0;
  613. sutSms.haveUnRead = 0;
  614. }
  615. void MessageFormatResponse(void)
  616. {
  617. unsigned short key=getKeyValue();
  618. if(MKEY_VALUE_MENU==key)
  619. {
  620. FormatSMS();
  621. uISetNextStatus(UIS_MENU_MAIL_SEL);
  622. }
  623. if(MKEY_VALUE_ESC==key)uISetNextStatus(UIS_MENU_MAIL_SEL);
  624. }
  625. void MessageOptionResponse(void)
  626. {
  627. char buf[30];
  628. unsigned short key;
  629. key=uiMenuResponse(&sutMenuSysSetup);
  630. if(MKEY_VALUE_MENU==key)
  631. {
  632. switch(sutMenuSysSetup.handle)
  633. {
  634. case 0:uISetNextStatus(UIS_MENU_MAIL_DEL);break;
  635. case 1:uISetNextStatus(UIS_MENU_MAIL_FORMAT);break;
  636. }
  637. }
  638. else if(MKEY_VALUE_ESC==key) uISetNextStatus(sutUIstatus.LastStatus);
  639. }
  640. void MessageOptionShow(char update)
  641. {
  642. static const char *apcMenuSysSetup[]=
  643. {"删除","格式化",""};
  644. if(update)
  645. {
  646. guiClearAll(guiGetBackColor());
  647. guiShowCaption(0,"文本选项",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  648. uiMenuShowBottomLine();
  649. uiMenuInit(&sutMenuSysSetup,apcMenuSysSetup, FONT_MODE_12X12);
  650. uiMenuShowBottomIndacitor("确定", "返回");
  651. }
  652. }
  653. //读取index短信全部内容到bufPt
  654. static void ReadSpecificSms(unsigned char *bufPt)
  655. {
  656. SUT_SMS_INFO_HEADER info;
  657. unsigned char *ptr;
  658. int fd;
  659. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR);
  660. if(fd<0){
  661. MSG_ERR(1,"%s read sms but open failed", SMS_FILE);
  662. return;
  663. }
  664. ptr=(unsigned char *)&info;
  665. nwy_sdk_fseek(fd, sutSms.ReadAddr[sutListBox.handle]+SMS_INFO_DETAIL_OFFSET, NWY_SEEK_SET);
  666. nwy_sdk_fread(fd,bufPt, SMS_SIGNAL_LEN);
  667. //sFlash_Read(bufPt, sutSms.ReadAddr[sutListBox.handle]+SMS_INFO_DETAIL_OFFSET, SMS_SIGNAL_LEN);
  668. nwy_sdk_fseek(fd, sutSms.ReadAddr[sutListBox.handle], NWY_SEEK_SET);
  669. nwy_sdk_fread(fd,ptr, 2);
  670. //sFlash_Read(ptr, sutSms.ReadAddr[sutListBox.handle],2);
  671. info.read = 0;
  672. nwy_sdk_fseek(fd, sutSms.ReadAddr[sutListBox.handle], NWY_SEEK_SET);
  673. nwy_sdk_fwrite(fd,ptr, 2);
  674. //sFlash_Write(ptr, sutSms.ReadAddr[sutListBox.handle], 2);
  675. if(sutSms.smsUnReadNum)
  676. {
  677. sutSms.smsUnReadNum--;
  678. if(sutSms.smsUnReadNum==0)
  679. sutSms.haveUnRead = 0;
  680. }
  681. if(fd>0) nwy_sdk_fclose(fd);
  682. }
  683. static unsigned short lastShowIndex=0;
  684. unsigned char smsDetail[SMS_SIGNAL_LEN];
  685. void MessageReadShow(char update)
  686. {
  687. if(update)
  688. {
  689. lastShowIndex=0;
  690. memset(smsDetail,0,sizeof(smsDetail));
  691. guiClearAll(guiGetBackColor());
  692. guiShowCaption(0,"消息阅读",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  693. ReadSpecificSms(smsDetail);
  694. MessageEditInit(&sutMEdit,8,UI_CONTENT_SHOW_Y+5, (char *)smsDetail);
  695. lastShowIndex=MessageEditShow(&sutMEdit,(char *)smsDetail+lastShowIndex);
  696. uiMenuShowBottomLine();
  697. #ifdef REPORT_SMS_VOICE
  698. uiMenuShowBottomIndacitor("播放", "返回");
  699. #else
  700. uiMenuShowBottomIndacitor(NULL, "返回");
  701. #endif
  702. return;
  703. }
  704. }
  705. void MessageReadResponse(void)
  706. {
  707. unsigned short len;
  708. unsigned short key=getKeyValue();
  709. if(MKEY_VALUE_ESC==getKeyValue())uISetNextStatus(UIS_MENU_MAIL_SEL);
  710. else if(MKEY_VALUE_MENU == key)
  711. {
  712. #ifdef REPORT_SMS_VOICE
  713. // ReadSpecificSms(smsDetail);
  714. // AnsiToStrUnicode(unicodeSms,sizeof(unicodeSms),(char *)smsDetail);
  715. // ModemSendAt("AT+ZTTS=1,\"");
  716. // ModemSendAt((char *)unicodeSms);
  717. // ModemSendAt("\"\r");
  718. #endif
  719. }else if(MKEY_VALUE_DOWN==key)
  720. {
  721. //printf("len:%d,%d\r\n",lastShowIndex,strlen(smsDetail));
  722. if(lastShowIndex<strlen(smsDetail))
  723. {
  724. guiClearRect(0,UI_CONTENT_SHOW_Y+5,GLCD_WIDTH-1,UI_BOTTOM_LINE_Y-2,guiGetBackColor());
  725. MessageEditInit(&sutMEdit,8,UI_CONTENT_SHOW_Y+5, (char *)smsDetail);
  726. len=MessageEditShow(&sutMEdit,(char *)smsDetail+lastShowIndex);
  727. if(len !=0) lastShowIndex+=len;
  728. else lastShowIndex=0;
  729. }
  730. }
  731. }
  732. void SwitchSMS_ToGB2312(void)
  733. {
  734. unsigned char *ptr;
  735. unsigned char tempSMS[SMS_SIGNAL_LEN+1];
  736. ptr = sutMess.GBKMess1+4;
  737. //source data:sutMess.GBKMess1+4;
  738. //data len:sutMess.len
  739. switch(sutMess.codeType)
  740. {
  741. case 2://unicode to gb2312
  742. memcpy(tempSMS, ptr, SMS_SIGNAL_LEN+1-4);
  743. //StrUnicodeToAnsi(ptr, sutMess.len, (const char *)tempSMS);//
  744. // unicodeToGbk(tempSMS, ptr, &sutMess.len);
  745. //len no change
  746. break;
  747. default:
  748. break;
  749. }
  750. }
  751. //向上存方式
  752. void IncomingSMS(void)
  753. {
  754. SUT_SMS_INFO_HEADER infosms;
  755. unsigned char *ptr,i;
  756. static unsigned short showCt;
  757. unsigned char *smsPtr;
  758. unsigned char tempyear;
  759. MY_CLOCK time;
  760. getMyClock(&time);
  761. if(0 == sutSms.smsEnable)
  762. {
  763. if(++showCt >= 500)
  764. MSG_ERR(1,"[SMS]Invalid");
  765. return;
  766. }
  767. if(0 != sutMess.Update) return;//消息播报处理完毕
  768. if(0 == sutSms.smsUpdate) return;//没有可存储处理
  769. sutSms.smsUpdate = 0;
  770. MSG_INFO(1,"[SMS]Store SMS");
  771. SmsScan(2);
  772. if(sutSms.poolFull)
  773. return;
  774. int fd;
  775. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR);
  776. if(fd<0){
  777. MSG_ERR(1,"%s open to save but failed",SMS_FILE);
  778. return;
  779. }
  780. SwitchSMS_ToGB2312();
  781. // SpeakerEnable();
  782. // ModemSendAt("AT+ZTTS=1,\"36653052B065886D6F60\"\r");
  783. //1.1
  784. ptr = (unsigned char*)&infosms;
  785. infosms.flag=1;
  786. infosms.read=1;
  787. tempyear=time.year%2000;
  788. infosms.recTime = (unsigned int)tempyear<<26 |
  789. (unsigned int)time.month<<22 |
  790. (unsigned int)time.day<<17 |
  791. (unsigned int)time.hour<<12 |
  792. (unsigned int)time.min<<6 |
  793. (unsigned int)time.sec<<0;
  794. infosms.len = sutMess.len;
  795. //保存头信息
  796. nwy_sdk_fseek(fd, sutSms.emptyAddr, NWY_SEEK_SET);
  797. nwy_sdk_fwrite(fd, ptr, SMS_INFO_REAL_LEN);
  798. //保存内容
  799. smsPtr = sutMess.GBKMess1+4;
  800. nwy_sdk_fseek(fd, sutSms.emptyAddr+SMS_INFO_DETAIL_OFFSET,NWY_SEEK_SET);
  801. nwy_sdk_fwrite(fd, smsPtr, SMS_SIGNAL_LEN);
  802. nwy_sdk_fclose(fd);
  803. sutSms.haveUnRead=1;
  804. sutSms.smsTotalNum ++;
  805. sutSms.smsUnReadNum ++;
  806. sutSms.newestAddr = sutSms.emptyAddr;
  807. //保存
  808. newPara.messageNewAddr=sutSms.newestAddr;
  809. //save sys
  810. sysIniSave();
  811. }
  812. void SMSHandle(void)
  813. {
  814. unsigned char temp1,temp2;
  815. unsigned short i;
  816. char buf[30];
  817. static unsigned int cnt=0;
  818. unsigned char *p;
  819. if(sutMess.startCntFlag)
  820. {
  821. // if(os_time_get() > cnt)
  822. // {//秒粗计数器
  823. // cnt=os_time_get()+100;
  824. // if(++sutMess.skipCnt>20)
  825. // {//时间到了
  826. // sutMess.startCntFlag=0;
  827. // SlwTrace(INF,"Timeout",1);
  828. // }
  829. // }
  830. }
  831. if(sutMess.Update == 1 || sutMess.Update == 2)
  832. {
  833. MSG_INFO(1,"TTS_LEN:%d",sutMess.len);
  834. p = sutMess.GBKMess1+4;
  835. // for(i=0;i<sutMess.len;i+=4)
  836. // {
  837. // temp1 = p[i+2];
  838. // temp2 = p[i+3];
  839. // p[i+2] = p[i];
  840. // p[i+3] = p[i+1];
  841. // p[i] = temp1;
  842. // p[i+1]=temp2;
  843. // }
  844. //p[sutMess.len]=0;
  845. ttsPlay(ENCODE_USER_UNICODE_BE, (char *)p);
  846. sutMess.Update=0;
  847. }
  848. }