message.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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,1,"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(sutSms.smsTotalNum>=1){
  466. ListBoxInitSMS(&sutListBox,sutSms.smsTotalNum,accIconFileName,0);
  467. uiMenuShowBottomIndacitor("查阅", "[P1]选项");
  468. }else{
  469. ListBoxItemNumShowSMS(0,0);
  470. uiMenuShowBottomIndacitor(NULL, "[P1]选项");
  471. }
  472. }
  473. }
  474. void DeleteSpecificSMS(void)
  475. {
  476. SUT_SMS_INFO_HEADER smsinfo,smsinfoTem,*info;
  477. unsigned char *ptr,*ptrTem,temp;
  478. unsigned int curAddr;
  479. int fd;
  480. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR);
  481. if(fd<0){
  482. MSG_ERR(1,"%s del open but failed", SMS_FILE);
  483. return;
  484. }
  485. ptr=(unsigned char *)&smsinfo;
  486. info=&smsinfo;
  487. //curAddr=SMS_START_ADDRESS+index*SMS_SIZE_PER;//要删除的消息地址
  488. curAddr=sutSms.ReadAddr[sutListBox.handle];
  489. nwy_sdk_fseek(fd, curAddr,NWY_SEEK_SET);
  490. nwy_sdk_fread(fd, ptr, sizeof( SUT_SMS_INFO_HEADER));
  491. //sFlash_Read(ptr, curAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  492. //0 更新数目
  493. if(1==smsinfo.read)
  494. {
  495. if(sutSms.smsUnReadNum)
  496. {
  497. sutSms.smsUnReadNum --;
  498. if(0==sutSms.smsUnReadNum)
  499. sutSms.haveUnRead =0;
  500. }
  501. }
  502. if(sutSms.smsTotalNum)
  503. sutSms.smsTotalNum --;
  504. if(sutSms.poolFull)
  505. sutSms.poolFull--;
  506. if(curAddr != sutSms.newestAddr){
  507. //1将当前要删除的消息的上下消息接连起来
  508. //1.1设置上一条消息的nextAddr
  509. ptrTem=(unsigned char *)&smsinfo.nextAddr;
  510. nwy_sdk_fseek(fd,smsinfo.upperAddr+SMS_INFO_REAL_LEN+4,NWY_SEEK_SET );
  511. nwy_sdk_fwrite(fd, ptrTem, 4);
  512. //sFlash_Write(ptrTem, smsinfo.upperAddr+SMS_INFO_REAL_LEN+4, 4);
  513. //1.2设置下一条消息的upperAddr
  514. ptrTem=(unsigned char *)&smsinfo.upperAddr;
  515. nwy_sdk_fseek(fd,smsinfo.nextAddr+SMS_INFO_REAL_LEN,NWY_SEEK_SET );
  516. nwy_sdk_fwrite(fd, ptrTem, 4);
  517. //sFlash_Write(ptrTem, smsinfo.nextAddr+SMS_INFO_REAL_LEN, 4);
  518. }
  519. //2设置要删除的消息的标志位
  520. temp=0;
  521. nwy_sdk_fseek(fd,curAddr,NWY_SEEK_SET );
  522. nwy_sdk_fwrite(fd, &temp, 1);
  523. //sFlash_Write(&temp, curAddr, 1);
  524. if(curAddr != sutSms.newestAddr)
  525. {
  526. //3把空的消息插到最新的那条消息的上面去,它的下面都会是连续的新消息,不要往下插
  527. nwy_sdk_fseek(fd, sutSms.newestAddr, NWY_SEEK_SET);
  528. nwy_sdk_fread(fd, ptr, sizeof( SUT_SMS_INFO_HEADER));
  529. //sFlash_Read(ptr, sutSms.newestAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  530. //3.1设置最新的消息的upperAddr
  531. nwy_sdk_fseek(fd, sutSms.newestAddr+SMS_INFO_REAL_LEN, NWY_SEEK_SET);
  532. nwy_sdk_fwrite(fd, (unsigned char *)&curAddr, 4);
  533. //sFlash_Write((uint8_t *)&curAddr, sutSms.newestAddr+SMS_INFO_REAL_LEN, 4);
  534. //3.2设置最新消息原来的上一条消息的nextAddr
  535. nwy_sdk_fseek(fd, smsinfo.upperAddr+SMS_INFO_REAL_LEN+4, NWY_SEEK_SET);
  536. nwy_sdk_fwrite(fd, (unsigned char *)&curAddr, 4);
  537. //sFlash_Write((uint8_t *)&curAddr, smsinfo.upperAddr+SMS_INFO_REAL_LEN+4, 4);
  538. //4设置被删除的消息的upperAddr and nextAddr
  539. //4.1设置upperAddr
  540. nwy_sdk_fseek(fd, curAddr+SMS_INFO_REAL_LEN, NWY_SEEK_SET);
  541. nwy_sdk_fwrite(fd, (unsigned char *)&smsinfo.upperAddr, 4);
  542. //sFlash_Write((uint8_t *)&smsinfo.upperAddr, curAddr+SMS_INFO_REAL_LEN, 4);
  543. //4.2设置nextAddr
  544. nwy_sdk_fseek(fd, curAddr+SMS_INFO_REAL_LEN+4, NWY_SEEK_SET);
  545. nwy_sdk_fwrite(fd, (unsigned char *)&sutSms.newestAddr, 4);
  546. //sFlash_Write((uint8_t *)&sutSms.newestAddr, curAddr+SMS_INFO_REAL_LEN+4, 4);
  547. }else
  548. {
  549. //如果是删除最新的那条消息,不用更改位置,只要将更改最新消息的地址即可
  550. sutSms.newestAddr = smsinfo.nextAddr;
  551. }
  552. if(fd>0) nwy_sdk_fclose(fd);
  553. }
  554. void MessageDeletResponse(void)
  555. {
  556. unsigned short key=getKeyValue();
  557. if(MKEY_VALUE_MENU==key)
  558. {
  559. if(sutSms.smsTotalNum>=1)
  560. DeleteSpecificSMS();
  561. uISetNextStatus(UIS_MENU_MAIL_SEL);
  562. }
  563. if(MKEY_VALUE_ESC==key)uISetNextStatus(UIS_MENU_MAIL_SEL);
  564. }
  565. void FormatSMS(void)
  566. {
  567. unsigned short i;
  568. unsigned char *ptr;
  569. SUT_SMS_INFO_HEADER info;
  570. unsigned int readAddr;
  571. unsigned char process;
  572. int fd;
  573. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR);
  574. if(fd<0){
  575. MSG_ERR(1,"%s format but open failed", SMS_FILE);
  576. return;
  577. }
  578. guiClearRect(0,44,GLCD_WIDTH,84,guiGetBackColor());
  579. guiDrawRect(29,50,128,66,1,guiGetForeColor());
  580. process=0;
  581. readAddr = sutSms.newestAddr;
  582. ptr=(unsigned char *)&info;
  583. for(i=0;i<SMS_TOTAL_NUM;i++)
  584. {
  585. nwy_sdk_fseek(fd, readAddr,NWY_SEEK_SET);
  586. nwy_sdk_fread(fd, ptr, sizeof(SUT_SMS_INFO_HEADER));
  587. //sFlash_Read(ptr, readAddr, sizeof(struct SUT_SMS_INFO_HEADER));
  588. if(info.flag == 1)
  589. {
  590. info.flag=0;
  591. nwy_sdk_fseek(fd, readAddr,NWY_SEEK_SET);
  592. nwy_sdk_fwrite(fd, ptr, 1);
  593. //sFlash_Write(ptr, readAddr, 1);
  594. }
  595. readAddr = info.nextAddr;
  596. if(i%10==0)
  597. {
  598. guiDrawVLine(52,64,29+process,1,guiGetForeColor());
  599. process++;
  600. }
  601. }
  602. if(fd>0) nwy_sdk_fclose(fd);
  603. sutSms.smsTotalNum = 0;
  604. sutSms.smsUnReadNum = 0;
  605. sutSms.haveUnRead = 0;
  606. }
  607. void MessageFormatResponse(void)
  608. {
  609. unsigned short key=getKeyValue();
  610. if(MKEY_VALUE_MENU==key)
  611. {
  612. FormatSMS();
  613. uISetNextStatus(UIS_MENU_MAIL_SEL);
  614. }
  615. if(MKEY_VALUE_ESC==key)uISetNextStatus(UIS_MENU_MAIL_SEL);
  616. }
  617. void MessageOptionResponse(void)
  618. {
  619. char buf[30];
  620. unsigned short key;
  621. key=uiMenuResponse(&sutMenuSysSetup);
  622. if(MKEY_VALUE_MENU==key)
  623. {
  624. switch(sutMenuSysSetup.handle)
  625. {
  626. case 0:uISetNextStatus(UIS_MENU_MAIL_DEL);break;
  627. case 1:uISetNextStatus(UIS_MENU_MAIL_FORMAT);break;
  628. }
  629. }
  630. else if(MKEY_VALUE_ESC==key) uISetNextStatus(sutUIstatus.LastStatus);
  631. }
  632. void MessageOptionShow(char update)
  633. {
  634. static const char *apcMenuSysSetup[]=
  635. {"删除","格式化",""};
  636. if(update)
  637. {
  638. guiClearAll(guiGetBackColor());
  639. guiShowCaption(0,"文本选项",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  640. uiMenuShowBottomLine();
  641. uiMenuInit(&sutMenuSysSetup,apcMenuSysSetup, FONT_MODE_12X12,true);
  642. uiMenuShowBottomIndacitor("确定", "返回");
  643. }
  644. }
  645. //读取index短信全部内容到bufPt
  646. static void ReadSpecificSms(unsigned char *bufPt)
  647. {
  648. SUT_SMS_INFO_HEADER info;
  649. unsigned char *ptr;
  650. int fd;
  651. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR);
  652. if(fd<0){
  653. MSG_ERR(1,"%s read sms but open failed", SMS_FILE);
  654. return;
  655. }
  656. ptr=(unsigned char *)&info;
  657. nwy_sdk_fseek(fd, sutSms.ReadAddr[sutListBox.handle]+SMS_INFO_DETAIL_OFFSET, NWY_SEEK_SET);
  658. nwy_sdk_fread(fd,bufPt, SMS_SIGNAL_LEN);
  659. //sFlash_Read(bufPt, sutSms.ReadAddr[sutListBox.handle]+SMS_INFO_DETAIL_OFFSET, SMS_SIGNAL_LEN);
  660. nwy_sdk_fseek(fd, sutSms.ReadAddr[sutListBox.handle], NWY_SEEK_SET);
  661. nwy_sdk_fread(fd,ptr, 2);
  662. //sFlash_Read(ptr, sutSms.ReadAddr[sutListBox.handle],2);
  663. info.read = 0;
  664. nwy_sdk_fseek(fd, sutSms.ReadAddr[sutListBox.handle], NWY_SEEK_SET);
  665. nwy_sdk_fwrite(fd,ptr, 2);
  666. //sFlash_Write(ptr, sutSms.ReadAddr[sutListBox.handle], 2);
  667. if(sutSms.smsUnReadNum)
  668. {
  669. sutSms.smsUnReadNum--;
  670. if(sutSms.smsUnReadNum==0)
  671. sutSms.haveUnRead = 0;
  672. }
  673. if(fd>0) nwy_sdk_fclose(fd);
  674. }
  675. static unsigned short lastShowIndex=0;
  676. unsigned char smsDetail[SMS_SIGNAL_LEN];
  677. void MessageReadShow(char update)
  678. {
  679. if(update)
  680. {
  681. lastShowIndex=0;
  682. memset(smsDetail,0,sizeof(smsDetail));
  683. guiClearAll(guiGetBackColor());
  684. guiShowCaption(0,"消息阅读",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_12X12);
  685. ReadSpecificSms(smsDetail);
  686. MessageEditInit(&sutMEdit,8,UI_CONTENT_SHOW_Y+5, (char *)smsDetail,FONT_MODE_16X16);
  687. lastShowIndex=MessageEditShow(&sutMEdit,(char *)smsDetail+lastShowIndex);
  688. uiMenuShowBottomLine();
  689. #ifdef REPORT_SMS_VOICE
  690. uiMenuShowBottomIndacitor("播放", "返回");
  691. #else
  692. uiMenuShowBottomIndacitor(NULL, "返回");
  693. #endif
  694. return;
  695. }
  696. }
  697. void MessageReadResponse(void)
  698. {
  699. unsigned short len;
  700. unsigned short key=getKeyValue();
  701. if(MKEY_VALUE_ESC==getKeyValue())uISetNextStatus(UIS_MENU_MAIL_SEL);
  702. else if(MKEY_VALUE_MENU == key)
  703. {
  704. #ifdef REPORT_SMS_VOICE
  705. // ReadSpecificSms(smsDetail);
  706. // AnsiToStrUnicode(unicodeSms,sizeof(unicodeSms),(char *)smsDetail);
  707. // ModemSendAt("AT+ZTTS=1,\"");
  708. // ModemSendAt((char *)unicodeSms);
  709. // ModemSendAt("\"\r");
  710. #endif
  711. }else if(MKEY_VALUE_DOWN==key)
  712. {
  713. //printf("len:%d,%d\r\n",lastShowIndex,strlen(smsDetail));
  714. if(lastShowIndex<strlen(smsDetail))
  715. {
  716. guiClearRect(0,UI_CONTENT_SHOW_Y+5,GLCD_WIDTH-1,UI_BOTTOM_LINE_Y-2,guiGetBackColor());
  717. MessageEditInit(&sutMEdit,8,UI_CONTENT_SHOW_Y+5, (char *)smsDetail,FONT_MODE_16X16);
  718. len=MessageEditShow(&sutMEdit,(char *)smsDetail+lastShowIndex);
  719. if(len !=0) lastShowIndex+=len;
  720. else lastShowIndex=0;
  721. }
  722. }
  723. }
  724. void SwitchSMS_ToGB2312(void)
  725. {
  726. unsigned char *ptr;
  727. unsigned char tempSMS[SMS_SIGNAL_LEN+1];
  728. ptr = sutMess.GBKMess1+4;
  729. //source data:sutMess.GBKMess1+4;
  730. //data len:sutMess.len
  731. switch(sutMess.codeType)
  732. {
  733. case 2://unicode to gb2312
  734. memcpy(tempSMS, ptr, SMS_SIGNAL_LEN+1-4);
  735. //StrUnicodeToAnsi(ptr, sutMess.len, (const char *)tempSMS);//
  736. unicodeToGbk(tempSMS, ptr, &sutMess.len);
  737. //len no change
  738. break;
  739. default:
  740. break;
  741. }
  742. }
  743. //向上存方式
  744. void IncomingSMS(void)
  745. {
  746. SUT_SMS_INFO_HEADER infosms;
  747. unsigned char *ptr,i;
  748. static unsigned short showCt;
  749. unsigned char *smsPtr;
  750. unsigned char tempyear;
  751. MY_CLOCK time;
  752. getMyClock(&time);
  753. if(0 == sutSms.smsEnable)
  754. {
  755. if(++showCt >= 500)
  756. MSG_ERR(1,"[SMS]Invalid");
  757. return;
  758. }
  759. if(0 != sutMess.Update) return;//消息播报处理完毕
  760. if(0 == sutSms.smsUpdate) return;//没有可存储处理
  761. sutSms.smsUpdate = 0;
  762. MSG_INFO(1,"[SMS]Store SMS");
  763. SmsScan(2);
  764. if(sutSms.poolFull)
  765. return;
  766. int fd;
  767. fd=nwy_sdk_fopen(SMS_FILE, NWY_RDWR);
  768. if(fd<0){
  769. MSG_ERR(1,"%s open to save but failed",SMS_FILE);
  770. return;
  771. }
  772. SwitchSMS_ToGB2312();
  773. // SpeakerEnable();
  774. // ModemSendAt("AT+ZTTS=1,\"36653052B065886D6F60\"\r");
  775. //1.1
  776. ptr = (unsigned char*)&infosms;
  777. infosms.flag=1;
  778. infosms.read=1;
  779. tempyear=time.year%2000;
  780. infosms.recTime = (unsigned int)tempyear<<26 |
  781. (unsigned int)time.month<<22 |
  782. (unsigned int)time.day<<17 |
  783. (unsigned int)time.hour<<12 |
  784. (unsigned int)time.min<<6 |
  785. (unsigned int)time.sec<<0;
  786. infosms.len = sutMess.len;
  787. //保存头信息
  788. nwy_sdk_fseek(fd, sutSms.emptyAddr, NWY_SEEK_SET);
  789. nwy_sdk_fwrite(fd, ptr, SMS_INFO_REAL_LEN);
  790. //保存内容
  791. smsPtr = sutMess.GBKMess1+4;
  792. nwy_sdk_fseek(fd, sutSms.emptyAddr+SMS_INFO_DETAIL_OFFSET,NWY_SEEK_SET);
  793. nwy_sdk_fwrite(fd, smsPtr, SMS_SIGNAL_LEN);
  794. nwy_sdk_fclose(fd);
  795. sutSms.haveUnRead=1;
  796. sutSms.smsTotalNum ++;
  797. sutSms.smsUnReadNum ++;
  798. sutSms.newestAddr = sutSms.emptyAddr;
  799. //保存
  800. newPara.messageNewAddr=sutSms.newestAddr;
  801. //save sys
  802. sysIniSave();
  803. }
  804. void SMSHandle(void)
  805. {
  806. unsigned char temp1,temp2;
  807. unsigned short i;
  808. char buf[30];
  809. static unsigned int cnt=0;
  810. unsigned char *p;
  811. if(sutMess.startCntFlag)
  812. {
  813. // if(os_time_get() > cnt)
  814. // {//秒粗计数器
  815. // cnt=os_time_get()+100;
  816. // if(++sutMess.skipCnt>20)
  817. // {//时间到了
  818. // sutMess.startCntFlag=0;
  819. // SlwTrace(INF,"Timeout",1);
  820. // }
  821. // }
  822. }
  823. if(sutMess.Update == 1 || sutMess.Update == 2)
  824. {
  825. MSG_INFO(1,"TTS_LEN:%d",sutMess.len);
  826. p = sutMess.GBKMess1+4;
  827. // for(i=0;i<sutMess.len;i+=4)
  828. // {
  829. // temp1 = p[i+2];
  830. // temp2 = p[i+3];
  831. // p[i+2] = p[i];
  832. // p[i+3] = p[i+1];
  833. // p[i] = temp1;
  834. // p[i+1]=temp2;
  835. // }
  836. //p[sutMess.len]=0;
  837. ttsPlay(ENCODE_USER_UNICODE_BE, (char *)p);
  838. sutMess.Update=0;
  839. }
  840. }