main.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  1. #include <sys/poll.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <spawn.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <assert.h>
  11. #include <time.h>
  12. #include <pthread.h>
  13. #include "hiredis/hiredis.h"
  14. #include "cjson/cJSON.h"
  15. #include "iniparser/iniparser.h"
  16. #include "log.h"
  17. #define Boolean int
  18. #define TRUE 1
  19. #define FALSE 0
  20. #define MAX_PIPE_BUFSIZE 256
  21. //KEYS
  22. #define UNKEY 0
  23. #define KEY 1
  24. #define KEY2_3 2
  25. #define KEY2_10 3
  26. #define OPEN 4
  27. #define CLOSED 4
  28. //SCHEDULE STATE
  29. #define ERROR -1
  30. #define STOP 0
  31. #define RUNNING 1
  32. #define PAUSED 2
  33. #define MAX_PRIORITY 10
  34. #define MAX_TRY 3
  35. //BARESIP_STATUS
  36. #define IDLE 0
  37. #define INUSE 1
  38. #define RING 2
  39. #define RINGING 3
  40. #define HOLD 4
  41. #define OFFLINE 5
  42. #define DIALING 6
  43. //BARESIP_REG
  44. #define REGISTER_FAIL 0
  45. #define REGISTER_OK 1
  46. //LED_STATUS
  47. #define Led_Off 0
  48. #define Led_On 1
  49. #define Led_Fast 2
  50. #define Led_Slow 3
  51. //REDIS_CHANNEL
  52. #define BARESIP_CHAN 0
  53. #define GPIO_CHAN 1
  54. //GPIO_STATUS
  55. #define GPIO_OFF 0
  56. #define GPIO_ON 1
  57. #define SPK_CONF "/etc/speaker.conf"
  58. #define VOL_CONF "/oem/etc/volctrl.conf"
  59. #define SIPLOGPATH "/userdata/system.csv"
  60. #define CDR_CSV "/userdata/cdr.csv"
  61. #define SCHEDULE_CONF "/oem/etc/schedule.conf"
  62. #define unix_socket_path "/tmp/redis.sock"
  63. //operator log type
  64. #define BTN "BUTTON"
  65. #define FUN "FUNCTION"
  66. #define STA "SIP STATE"
  67. #define SCHED "SCHEDULE"
  68. #define GPIO_RELAY "gpio112"
  69. #define VAR_IP "${ip}"
  70. #define VAR_MAC "${mac}"
  71. #define VAR_UA "${ua}"
  72. #define VAR_NUM "${number}"
  73. #define AUDIO_ON "{\"name\": \"SCHEDULER-AUDIO\", \"action\": \"on\"}"
  74. #define AUDIO_OFF "{\"name\": \"SCHEDULER-AUDIO\", \"action\": \"off\"}"
  75. struct date{ //声明结构体date用于表示日期
  76. int year;
  77. int month;
  78. int day;
  79. };
  80. struct VLC_INFO{
  81. pid_t pid;
  82. int prio;
  83. char param[96];
  84. };
  85. typedef struct _playInfo{
  86. int key_id;
  87. int prio;
  88. int vol;
  89. char file[64];
  90. char times_str[8];
  91. char enBeep[8];
  92. char enSrceen[8];
  93. } PlayInfo;
  94. struct CALL_LOG{
  95. char date[16];
  96. char start[16];
  97. char end[16];
  98. char acc[32];
  99. char num[32];
  100. char direct[8];
  101. char status[16];
  102. time_t answerTime;
  103. int duration;
  104. char desc[64];
  105. };
  106. typedef struct {
  107. int in_use[5];
  108. redisContext *context[5];
  109. const char *socket_path;
  110. time_t last_activity[5];
  111. pthread_mutex_t lock;
  112. } redis_client_t;
  113. static int BARESIP_STATUS = OFFLINE;
  114. static int CURRENT_KEY = UNKEY;
  115. static int DEBUG = FALSE;
  116. static int ACCOUNT1_REG = REGISTER_FAIL;
  117. static int ACCOUNT2_REG = REGISTER_FAIL;
  118. static int ACCOUNT3_REG = REGISTER_FAIL;
  119. static int ACCOUNTP2P_REG = REGISTER_FAIL;
  120. static int press_down = FALSE;
  121. static int CURRENT_CALLS = 0;
  122. static int regcount = 0;
  123. static char DTMF_STRING[32] = "\0";
  124. static int SCHEDULE[31];
  125. struct VLC_INFO vlcInfo[64];
  126. struct CALL_LOG callInfo;
  127. static redis_client_t *redisClient;
  128. redis_client_t* create_redis_pool_client(const char *socket_path) {
  129. redis_client_t *client = (redis_client_t *) malloc(sizeof(redis_client_t));
  130. client->socket_path = socket_path;
  131. pthread_mutex_init(&client->lock, NULL);
  132. for(int i = 0; i < 5; i++)
  133. {
  134. redisContext *c = redisConnectUnix(socket_path);
  135. if (c == NULL || c->err) {
  136. if (c) {
  137. printf("Connection error: %s\n", c->errstr);
  138. redisFree(c);
  139. } else {
  140. printf("Connection error: can't allocate redis context\n");
  141. }
  142. // 可以选择重试或退出
  143. continue;
  144. }
  145. client->context[i] = c;
  146. client->in_use[i] = FALSE;
  147. client->last_activity[i] = time(NULL);
  148. }
  149. return client;
  150. }
  151. const int redis_pool_get_context(redis_client_t *client) {
  152. pthread_mutex_lock(&client->lock);
  153. redisContext *context = NULL;
  154. int idx = -1;
  155. for(int i = 0;i < 5; i++)
  156. {
  157. if(!client->in_use[i])
  158. {
  159. idx = i;
  160. client->in_use[idx] = TRUE;
  161. context = client->context[idx];
  162. redisReply *reply = redisCommand(context, "PING");
  163. if (reply == NULL || context->err) {
  164. // 连接失效,尝试重连
  165. if(DEBUG)
  166. printf( "redis connect failed, retry!\n" );
  167. redisFree(context);
  168. context = redisConnectUnix(client->socket_path);
  169. client->context[idx] = context;
  170. }
  171. freeReplyObject(reply);
  172. client->last_activity[idx] = time(NULL);
  173. break;
  174. }
  175. }
  176. pthread_mutex_unlock(&client->lock);
  177. if (context == NULL) {
  178. printf("No available connections in pool\n");
  179. }
  180. return idx;
  181. }
  182. // 释放连接
  183. void redis_pool_release_connection(redis_client_t *client, const int id) {
  184. pthread_mutex_lock(&client->lock);
  185. client->in_use[id] = FALSE;
  186. pthread_mutex_unlock(&client->lock);
  187. }
  188. void redis_command_safe(const char *format, const char *value) {
  189. const int id = redis_pool_get_context(redisClient);
  190. if (id != -1) {
  191. redisReply *reply = (redisReply *) redisCommand(redisClient->context[id], format, value);
  192. if (reply) {
  193. // 处理回复
  194. freeReplyObject(reply);
  195. }
  196. redis_pool_release_connection(redisClient, id);
  197. }
  198. }
  199. int ensure_connected(redisContext *context) {
  200. if (context == NULL || context->err) {
  201. if (context) {
  202. redisFree(context);
  203. }
  204. context = redisConnectUnix(unix_socket_path);
  205. if (context == NULL || context->err) {
  206. return -1; // 连接失败
  207. }
  208. }
  209. return 0;
  210. }
  211. void reset_callInfo()
  212. {
  213. memset(&callInfo,0,sizeof(callInfo));
  214. callInfo.duration = 0;
  215. }
  216. void *write_opnlog()
  217. {
  218. char * pKey = "writelog-channel";
  219. redisReply *pReply, *pSubReply;
  220. redisContext *redisLogContext = NULL;
  221. redisLogContext = redisConnectUnix(unix_socket_path);
  222. restart:
  223. FILE *logFp = fopen(SIPLOGPATH, "a");
  224. log_add_fp(logFp, LOG_INFO);
  225. while(1)
  226. {
  227. pReply = NULL;
  228. if (ensure_connected(redisLogContext) == 0) {
  229. pReply = redisCommand( redisLogContext, "BRPOP %s 60", pKey );
  230. if(pReply != NULL && pReply->type == REDIS_REPLY_ARRAY){
  231. pSubReply = NULL;
  232. if ( pReply->elements == 2 )
  233. {
  234. pSubReply = pReply->element[1];
  235. if(DEBUG)
  236. printf( "opnLog [%s]\n", pSubReply->str );
  237. if(strcmp(pSubReply->str, "log_clean") == 0)
  238. {
  239. fclose(logFp);
  240. freeReplyObject( pReply );
  241. sleep(5);
  242. goto restart;
  243. }
  244. else
  245. log_info("%s", pSubReply->str);
  246. }
  247. }
  248. }
  249. if(pReply != NULL) freeReplyObject( pReply );
  250. usleep(200*1000);
  251. }
  252. redisFree( redisLogContext );
  253. }
  254. void writeLog(char *type,char *action, char *remark)
  255. {
  256. char logstr[256];
  257. sprintf(logstr, "%s,%s,%s", type, action, remark);
  258. redis_command_safe("LPUSH writelog-channel %s", logstr);
  259. }
  260. //发送状态指令
  261. void state_event(char *info)
  262. {
  263. redis_command_safe("PUBLISH volume-event-channel %s", info);
  264. }
  265. //发送屏幕显示指令
  266. void screen_control(char *cmd)
  267. {
  268. redis_command_safe("PUBLISH screen-channel %s", cmd);
  269. }
  270. void insert_call_log()
  271. {
  272. FILE *cdr_fp = fopen(CDR_CSV, "a");
  273. //日期,开始时间,结束时间,账号,号码,通话时长,方向,状态,描述
  274. fprintf(cdr_fp, "%s,%s,%s,%s,%s,%d,%s,%s,%s\n",
  275. callInfo.date, callInfo.start, callInfo.end, callInfo.acc, callInfo.num, callInfo.duration,
  276. callInfo.direct, callInfo.status, callInfo.desc);
  277. fclose(cdr_fp);
  278. reset_callInfo();
  279. }
  280. void setIncomingCallInfo(char *accuri, char *num)
  281. {
  282. char acc[32];
  283. time_t nSeconds;
  284. struct tm *pTM;
  285. int year, month, hour;
  286. time(&nSeconds);
  287. pTM = localtime(&nSeconds);
  288. year = pTM->tm_year + 1900;
  289. month = pTM->tm_mon + 1;
  290. memset(acc,0,sizeof(acc));
  291. strcpy(callInfo.direct,"in");
  292. sprintf(callInfo.date, "%d-%02d-%02d", year, month, pTM->tm_mday);
  293. sprintf(callInfo.start, "%02d:%02d:%02d", pTM->tm_hour, pTM->tm_min, pTM->tm_sec);
  294. //strncpy(acc, accuri, strchr(accuri,'@') - accuri);
  295. //strcpy(callInfo.acc, acc + 4);
  296. strcpy(callInfo.acc, accuri);
  297. strcpy(callInfo.num, num);
  298. strcpy(callInfo.status, "MISSED CALL");
  299. }
  300. void setOutgoingCallInfo(char *accuri, char *num)
  301. {
  302. char acc[32];
  303. time_t nSeconds;
  304. struct tm *pTM;
  305. int year, month, hour;
  306. time(&nSeconds);
  307. pTM = localtime(&nSeconds);
  308. year = pTM->tm_year + 1900;
  309. month = pTM->tm_mon + 1;
  310. memset(acc,0,sizeof(acc));
  311. strcpy(callInfo.direct,"out");
  312. sprintf(callInfo.date, "%d-%02d-%02d", year, month, pTM->tm_mday);
  313. sprintf(callInfo.start, "%02d:%02d:%02d", pTM->tm_hour, pTM->tm_min, pTM->tm_sec);
  314. //strncpy(acc, accuri, strchr(accuri,'@') - accuri);
  315. //strcpy(callInfo.acc, acc + 4);
  316. strcpy(callInfo.acc, accuri);
  317. strcpy(callInfo.num, num);
  318. strcpy(callInfo.status, "NO ANSWER");
  319. }
  320. void setAnswerCallInfo()
  321. {
  322. time_t nSeconds;
  323. time(&nSeconds);
  324. callInfo.answerTime = nSeconds;
  325. strcpy(callInfo.status, "ANSWERED");
  326. }
  327. void setClosedCallInfo(char *param)
  328. {
  329. time_t nSeconds;
  330. struct tm *pTM;
  331. time(&nSeconds);
  332. pTM = localtime(&nSeconds);
  333. sprintf(callInfo.end, "%02d:%02d:%02d", pTM->tm_hour, pTM->tm_min, pTM->tm_sec);
  334. strcpy(callInfo.desc, param);
  335. if(callInfo.answerTime)
  336. {
  337. callInfo.duration = nSeconds - callInfo.answerTime;
  338. }
  339. insert_call_log();
  340. }
  341. /*
  342. * @file 文件路径
  343. * @value 命令结果指针
  344. * @lenth 存储结果长度
  345. */
  346. int GetCmdValue(char *file, char *key, char *value, int lenth)
  347. {
  348. dictionary * ini ;
  349. ini = iniparser_load(file);
  350. if (ini==NULL) {
  351. fprintf(stderr, "cannot parse file: %s\n", file);
  352. return FALSE ;
  353. }
  354. strcpy(value, iniparser_getstring(ini, key, ""));
  355. if(DEBUG)
  356. printf("keystr: \"%s\" ; value: %s.\n", key, value);
  357. iniparser_freedict(ini);
  358. return TRUE;
  359. }
  360. char *strrpl(char *s, const char *s1, const char *s2)
  361. {
  362. char *ptr;
  363. while (ptr = strstr(s, s1)) /* 如果在s中找到s1 */
  364. {
  365. memmove(ptr + strlen(s2) , ptr + strlen(s1), strlen(ptr) - strlen(s1) + 1);
  366. memcpy(ptr, &s2[0], strlen(s2));
  367. }
  368. return s;
  369. }
  370. //控制功放的开启/关闭,TRUE/FALSE
  371. void amplifier_switch(Boolean enable){
  372. if(enable)
  373. system("echo 1 > /sys/class/gpio/gpio5/value");
  374. else
  375. system("echo 0 > /sys/class/gpio/gpio5/value");
  376. }
  377. Boolean get_ip(char * pv)
  378. {
  379. FILE * fp;
  380. char cmdbuf[128], value[32];
  381. char *tmp=NULL;
  382. sprintf(cmdbuf,"/sbin/ifconfig eth1 | grep 'inet ' | awk '{print $2}'");
  383. fp = popen(cmdbuf, "r");
  384. if (fp == NULL) {
  385. // printf("Fail to open pipe\n");
  386. return FALSE;
  387. }
  388. memset(value,0,sizeof(value));
  389. if (!feof(fp) && fgets(value,32,fp) != NULL)
  390. ;
  391. // printf("value:%s\n", value);
  392. else {
  393. printf("ip wrong\n");
  394. return FALSE;
  395. }
  396. pclose(fp);
  397. if(strlen(value) < 10)
  398. return FALSE;
  399. if(tmp = strstr(value,"\n"))
  400. {
  401. *tmp = '\0';
  402. }
  403. strcpy(pv,value);
  404. return TRUE;
  405. }
  406. Boolean get_mac(char * pv)
  407. {
  408. FILE * fp;
  409. char cmdbuf[128], value[32];
  410. char *tmp=NULL;
  411. sprintf(cmdbuf,"ifconfig eth1 | grep ether | tr -d : | awk '{print $2}'");
  412. fp = popen(cmdbuf, "r");
  413. if (fp == NULL) {
  414. return FALSE;
  415. }
  416. memset(value,0,sizeof(value));
  417. if (!feof(fp) && fgets(value,32,fp) != NULL)
  418. ;
  419. // printf("value:%s\n", value);
  420. else {
  421. printf("ip wrong\n");
  422. return FALSE;
  423. }
  424. pclose(fp);
  425. if(strlen(value) < 10)
  426. return FALSE;
  427. if(tmp = strstr(value,"\n"))
  428. {
  429. *tmp = '\0';
  430. }
  431. strcpy(pv,value);
  432. return TRUE;
  433. }
  434. int PlayIP()
  435. {
  436. //add by ssc 2019-12-26
  437. char language[32];
  438. GetCmdValue(SPK_CONF, "system:language", language, sizeof(language));
  439. char ipaddr[16],cmd_line[240];
  440. system("/usr/bin/amixer -q sset 'Master',0 29");
  441. amplifier_switch(TRUE); //开启功放
  442. if(!get_ip(ipaddr)) {
  443. if (strcmp(language, "en") == 0){
  444. system("mpg123 -r 48000 /usr/share/sounds/en/get_ip_failed.mp3");
  445. }else{
  446. system("mpg123 -r 48000 /usr/share/sounds/cn/get_ip_failed.mp3");
  447. }
  448. return 0;
  449. }
  450. if (strcmp(language, "en") == 0){
  451. strcpy(cmd_line,"cd /usr/share/sounds/en/;mpg123 -r 48000 ");
  452. }else{
  453. strcpy(cmd_line,"cd /usr/share/sounds/cn/;mpg123 -r 48000 ");
  454. }
  455. int i;
  456. for (i=0;i<strlen(ipaddr);i++){
  457. switch(ipaddr[i])
  458. {
  459. case '0':
  460. strcat(cmd_line,"digit-0.mp3 ");
  461. break;
  462. case '1':
  463. strcat(cmd_line,"digit-1.mp3 ");
  464. break;
  465. case '2':
  466. strcat(cmd_line,"digit-2.mp3 ");
  467. break;
  468. case '3':
  469. strcat(cmd_line,"digit-3.mp3 ");
  470. break;
  471. case '4':
  472. strcat(cmd_line,"digit-4.mp3 ");
  473. break;
  474. case '5':
  475. strcat(cmd_line,"digit-5.mp3 ");
  476. break;
  477. case '6':
  478. strcat(cmd_line,"digit-6.mp3 ");
  479. break;
  480. case '7':
  481. strcat(cmd_line,"digit-7.mp3 ");
  482. break;
  483. case '8':
  484. strcat(cmd_line,"digit-8.mp3 ");
  485. break;
  486. case '9':
  487. strcat(cmd_line,"digit-9.mp3 ");
  488. break;
  489. case '.':
  490. strcat(cmd_line,"dian.mp3 ");
  491. break;
  492. }
  493. }
  494. // printf("cmd-line:%s\n", cmd_line);
  495. system(cmd_line);
  496. amplifier_switch(FALSE); //关闭功放
  497. system("/etc/scripts/set_volume.sh resume");
  498. writeLog( FUN, "Play IP Address", ipaddr );
  499. return 0;
  500. }
  501. void action_url(char *url)
  502. {
  503. char cmd[320];
  504. sprintf(cmd,"curl -k --connect-timeout 5 '%s' 2>/dev/null &",url);
  505. if(DEBUG)
  506. printf( "Command: %s\n", cmd);
  507. system(cmd);
  508. }
  509. void playBeep()
  510. {
  511. char beep[8];
  512. GetCmdValue(VOL_CONF, "volume:key_beep", beep, sizeof(beep));
  513. if(strcmp(beep, "yes") == 0)
  514. {
  515. if(BARESIP_STATUS == IDLE || BARESIP_STATUS == OFFLINE)
  516. system("/usr/sbin/redis-cli PUBLISH volume-event-channel '{\"name\": \"SIP\", \"action\": \"on\"}'");
  517. system("mpg123 /home/new-speaker/target/share/sounds/beep.mp3");
  518. usleep(400*1000);
  519. if(BARESIP_STATUS == IDLE || BARESIP_STATUS == OFFLINE)
  520. system("/usr/sbin/redis-cli PUBLISH volume-event-channel '{\"name\": \"SIP\", \"action\": \"off\"}'"); //关闭功放
  521. }
  522. }
  523. //发送控制指令
  524. void control_cmd(int channel, char *cmd)
  525. {
  526. char redisCmd[256];
  527. switch (channel)
  528. {
  529. case BARESIP_CHAN:
  530. if(DEBUG)
  531. printf( "control-channel [%s]\n", cmd );
  532. redis_command_safe("LPUSH control-channel %s", cmd );
  533. break;
  534. case GPIO_CHAN:
  535. if(DEBUG)
  536. printf( "output-channel [%s]\n", cmd );
  537. redis_command_safe("LPUSH output-channel %s", cmd );
  538. break;
  539. default:
  540. redis_command_safe("LPUSH control-channel %s", cmd );
  541. break;
  542. }
  543. }
  544. void relayControl(char *id, int enable){
  545. char cmd[64];
  546. int channel = GPIO_CHAN;
  547. if(enable){
  548. char output_type[32];
  549. GetCmdValue(SPK_CONF, "relay_ctrl:type", output_type, sizeof(output_type));
  550. sprintf(cmd,"{\"id\":\"%s\",\"type\":\"%s\"}",id,output_type);
  551. control_cmd(channel,cmd);
  552. }
  553. else
  554. {
  555. sprintf(cmd,"{\"id\":\"%s\",\"type\":\"Off\"}",id);
  556. control_cmd(channel,cmd);
  557. }
  558. }
  559. void screenControl(int enable){
  560. char cmd[64], output_type[16];
  561. GetCmdValue(SPK_CONF, "screen_ctrl:type", output_type, sizeof(output_type));
  562. if(enable){
  563. sprintf(cmd,"{\"action\": \"on\", \"id\": %s}",output_type);
  564. screen_control(cmd);
  565. }
  566. else
  567. {
  568. sprintf(cmd,"{\"action\": \"off\", \"id\": %s}",output_type);
  569. screen_control(cmd);
  570. }
  571. }
  572. //relay延时控制
  573. void *relayDelayControl()
  574. {
  575. char delay[8];
  576. GetCmdValue(SPK_CONF, "relay_ctrl:duration", delay, sizeof(delay));
  577. usleep(atoi(delay)*1000*1000);
  578. relayControl(GPIO_RELAY, GPIO_OFF);
  579. if(DEBUG)
  580. printf( "GPIO_RELAY set off\n" );
  581. }
  582. //srceen延时控制
  583. void *screenDelayControl()
  584. {
  585. char delay[8];
  586. GetCmdValue(SPK_CONF, "screen_ctrl:duration", delay, sizeof(delay));
  587. usleep(atoi(delay)*1000*1000);
  588. screenControl(FALSE);
  589. if(DEBUG)
  590. printf( "screen set off\n" );
  591. }
  592. void *key1ScreenDelayControl()
  593. {
  594. char delay[8], srceen_id[16], strtmp[64];
  595. GetCmdValue(SPK_CONF, "intercom:screen_duration", delay, sizeof(delay));
  596. usleep(atoi(delay)*1000*1000);
  597. GetCmdValue(SPK_CONF, "intercom:trigger_id", srceen_id, sizeof(srceen_id));
  598. sprintf(strtmp,"{\"action\": \"off\", \"id\": %s}",srceen_id);
  599. screen_control(strtmp);
  600. if(DEBUG)
  601. printf( "screen set off\n" );
  602. }
  603. void *key2ScreenDelayControl()
  604. {
  605. char delay[8], srceen_id[16], strtmp[64];
  606. GetCmdValue(SPK_CONF, "intercom:screen_2_duration", delay, sizeof(delay));
  607. usleep(atoi(delay)*1000*1000);
  608. GetCmdValue(SPK_CONF, "intercom:trigger_2_id", srceen_id, sizeof(srceen_id));
  609. sprintf(strtmp,"{\"action\": \"off\", \"id\": %s}",srceen_id);
  610. screen_control(strtmp);
  611. if(DEBUG)
  612. printf( "screen set off\n" );
  613. }
  614. //呼入触发
  615. void incomingTrigger()
  616. {
  617. char relay_call_state[8],relay_state_info[16],relay_mode[16];
  618. GetCmdValue(SPK_CONF, "relay_ctrl:call_state", relay_call_state, sizeof(relay_call_state));
  619. GetCmdValue(SPK_CONF, "relay_ctrl:state_info", relay_state_info, sizeof(relay_state_info));
  620. if( strcmp(relay_call_state, "yes") == 0 && ( strcmp(relay_state_info, "incoming") == 0 || strcmp(relay_state_info, "both") == 0 ) )
  621. {
  622. relayControl(GPIO_RELAY, GPIO_ON);
  623. if(DEBUG)
  624. printf( "GPIO_RELAY set on\n" );
  625. //gpio触发后判断是否为延时复位
  626. GetCmdValue(SPK_CONF, "relay_ctrl:mode", relay_mode, sizeof(relay_mode));
  627. if( strcmp(relay_mode, "delay") == 0 )
  628. {
  629. int ret_thrd_delay;
  630. pthread_t thread_delay;
  631. ret_thrd_delay = pthread_create(&thread_delay, NULL, relayDelayControl, NULL);
  632. if (ret_thrd_delay != 0) {
  633. printf("create thread_delay relayDelayControl failed\n");
  634. return;
  635. }
  636. pthread_detach(thread_delay);
  637. }
  638. }
  639. }
  640. //呼出触发
  641. void outgoingTrigger()
  642. {
  643. char relay_call_state[8],relay_state_info[16],relay_mode[16];
  644. char screen_call_state[8], screen_state_info[16], screen_mode[16];
  645. GetCmdValue(SPK_CONF, "relay_ctrl:call_state", relay_call_state, sizeof(relay_call_state));
  646. GetCmdValue(SPK_CONF, "relay_ctrl:state_info", relay_state_info, sizeof(relay_state_info));
  647. if( strcmp(relay_call_state, "yes") == 0 && ( strcmp(relay_state_info, "outgoing") == 0 || strcmp(relay_state_info, "both") == 0 ) )
  648. {
  649. relayControl(GPIO_RELAY, GPIO_ON);
  650. if(DEBUG)
  651. printf( "GPIO_RELAY set on\n" );
  652. //gpio触发后判断是否为延时复位
  653. GetCmdValue(SPK_CONF, "relay_ctrl:mode", relay_mode, sizeof(relay_mode));
  654. if( strcmp(relay_mode, "delay") == 0 )
  655. {
  656. int ret_thrd_delay;
  657. pthread_t thread_delay;
  658. ret_thrd_delay = pthread_create(&thread_delay, NULL, relayDelayControl, NULL);
  659. if (ret_thrd_delay != 0) {
  660. printf("create thread_delay relayDelayControl failed\n");
  661. return;
  662. }
  663. pthread_detach(thread_delay);
  664. }
  665. }
  666. }
  667. //应答触发
  668. void answeredTrigger()
  669. {
  670. char cmd[MAX_PIPE_BUFSIZE];
  671. char local_beep[4], local_beep_file[64], local_beep_volume[4];
  672. char remote_beep[4], remote_beep_file[64], remote_beep_volume[4];
  673. char relay_call_state[8],relay_state_info[16],relay_mode[16];
  674. char screen_call_state[8], screen_state_info[16], screen_mode[16];
  675. setAnswerCallInfo();
  676. //判断RELAY是否需要应答复位
  677. GetCmdValue(SPK_CONF, "relay_ctrl:mode", relay_mode, sizeof(relay_mode));
  678. if( strcmp(relay_mode, "answered") == 0 )
  679. {
  680. relayControl(GPIO_RELAY, GPIO_OFF);
  681. if(DEBUG)
  682. printf( "GPIO_RELAY set off\n" );
  683. }
  684. else
  685. {
  686. GetCmdValue(SPK_CONF, "relay_ctrl:call_state", relay_call_state, sizeof(relay_call_state));
  687. GetCmdValue(SPK_CONF, "relay_ctrl:state_info", relay_state_info, sizeof(relay_state_info));
  688. if( strcmp(relay_call_state, "yes") == 0 && strcmp(relay_state_info, "answered") == 0 )
  689. {
  690. relayControl(GPIO_RELAY, GPIO_ON);
  691. if(DEBUG)
  692. printf( "GPIO_RELAY set on\n" );
  693. //gpio触发后判断是否为延时复位
  694. if( strcmp(relay_mode, "delay") == 0 )
  695. {
  696. int ret_thrd_delay;
  697. pthread_t thread_delay;
  698. ret_thrd_delay = pthread_create(&thread_delay, NULL, relayDelayControl, NULL);
  699. if (ret_thrd_delay != 0) {
  700. printf("create thread_delay relayDelayControl failed\n");
  701. return;
  702. }
  703. pthread_detach(thread_delay);
  704. }
  705. }
  706. }
  707. //触发应答提示音播放
  708. GetCmdValue(SPK_CONF, "sip_function:answer_local_beep", local_beep, sizeof(local_beep));
  709. if(strcmp(local_beep, "yes") == 0)
  710. {
  711. GetCmdValue(SPK_CONF, "sip_function:answer_local_beep_file", local_beep_file, sizeof(local_beep_file));
  712. GetCmdValue(SPK_CONF, "sip_function:answer_local_beep_volume", local_beep_volume, sizeof(local_beep_volume));
  713. sprintf(cmd,"{\"cmd\":\"mixausrc_dec_start\",\"data\":\"aufile /usr/share/baresip/%s 10 %s\"}", local_beep_file, local_beep_volume);
  714. control_cmd(BARESIP_CHAN, cmd);
  715. }
  716. GetCmdValue(SPK_CONF, "sip_function:answer_remote_beep", remote_beep, sizeof(remote_beep));
  717. if(strcmp(remote_beep, "yes") == 0)
  718. {
  719. GetCmdValue(SPK_CONF, "sip_function:answer_remote_beep_file", remote_beep_file, sizeof(remote_beep_file));
  720. GetCmdValue(SPK_CONF, "sip_function:answer_remote_beep_volume", remote_beep_volume, sizeof(remote_beep_volume));
  721. sprintf(cmd,"{\"cmd\":\"mixausrc_enc_start\",\"data\":\"aufile /usr/share/baresip/%s 10 %s\"}", remote_beep_file, remote_beep_volume);
  722. control_cmd(BARESIP_CHAN, cmd);
  723. }
  724. }
  725. //挂断触发
  726. void hangupTrigger(char *param)
  727. {
  728. char cmd[MAX_PIPE_BUFSIZE];
  729. char relay_call_state[8],relay_state_info[16],relay_mode[16];
  730. char call_state[8],state_info[16],output_mode[16], beep[4], beep_file[64], beep_volume[4];
  731. char screen_call_state[8], screen_state_info[16], screen_mode[16];
  732. //触发挂断提示音播放
  733. GetCmdValue(SPK_CONF, "sip_function:hangup_beep", beep, sizeof(beep));
  734. if(strcmp(beep, "yes") == 0)
  735. {
  736. GetCmdValue(SPK_CONF, "sip_function:hangup_beep_file", beep_file, sizeof(beep_file));
  737. GetCmdValue(SPK_CONF, "sip_function:hangup_beep_volume", beep_volume, sizeof(beep_volume));
  738. sprintf(cmd,"{\"cmd\":\"setaudiotempvol\",\"data\":\"%s\"}",beep_volume);
  739. control_cmd(BARESIP_CHAN, cmd);
  740. sprintf(cmd,"{\"cmd\":\"play\",\"data\":\"%s:0\"}", beep_file);
  741. control_cmd(BARESIP_CHAN, cmd);
  742. }
  743. setClosedCallInfo(param);
  744. //判断RELAY是否需要挂断复位
  745. GetCmdValue(SPK_CONF, "relay_ctrl:mode", relay_mode, sizeof(relay_mode));
  746. if( strcmp(relay_mode, "hangup") == 0 )
  747. {
  748. relayControl(GPIO_RELAY, GPIO_OFF);
  749. if(DEBUG)
  750. printf( "GPIO_RELAY set off\n" );
  751. }
  752. else
  753. {
  754. //否则判断是否需要执行挂断触发
  755. GetCmdValue(SPK_CONF, "relay_ctrl:call_state", relay_call_state, sizeof(relay_call_state));
  756. GetCmdValue(SPK_CONF, "relay_ctrl:state_info", relay_state_info, sizeof(relay_state_info));
  757. if( strcmp(relay_call_state, "yes") == 0){
  758. if(strcmp(relay_state_info, "hangup") == 0 )
  759. {
  760. relayControl(GPIO_RELAY, GPIO_ON);
  761. if(DEBUG)
  762. printf( "GPIO_RELAY set on\n" );
  763. //gpio触发后延时复位
  764. int ret_thrd_delay;
  765. pthread_t thread_delay;
  766. ret_thrd_delay = pthread_create(&thread_delay, NULL, relayDelayControl, NULL);
  767. if (ret_thrd_delay != 0) {
  768. printf("create thread_delay relayDelayControl failed\n");
  769. return;
  770. }
  771. pthread_detach(thread_delay);
  772. }
  773. else
  774. {
  775. relayControl(GPIO_RELAY, GPIO_OFF);
  776. if(DEBUG)
  777. printf( "GPIO_RELAY set off\n" );
  778. }
  779. }
  780. }
  781. }
  782. //DTMF检测
  783. void dtmfDetect(char *dtmf)
  784. {
  785. char dtmf_event[8],dtmf_code[32],relay_mode[16];
  786. char srceen_dtmf_event[8],srceen_dtmf_code[32],srceen_mode[16];
  787. char dtmf_tmp[32] = "\0";
  788. int dtmflen,codelen,srceen_codelen, reset = FALSE;
  789. GetCmdValue(SPK_CONF, "relay_ctrl:dtmf", dtmf_event, sizeof(dtmf_event));
  790. GetCmdValue(SPK_CONF, "screen_ctrl:dtmf", srceen_dtmf_event, sizeof(srceen_dtmf_event));
  791. if( strcmp(dtmf_event, "yes") == 0 || strcmp(srceen_dtmf_event, "yes") == 0 )
  792. {
  793. strcat(DTMF_STRING,dtmf);
  794. if(DEBUG)
  795. printf( "DTMF is [%s]\n", DTMF_STRING );
  796. dtmflen = strlen(DTMF_STRING);
  797. if(strcmp(dtmf_event, "yes") == 0 )
  798. {
  799. GetCmdValue(SPK_CONF, "relay_ctrl:dtmf_code", dtmf_code, sizeof(dtmf_code));
  800. codelen = strlen(dtmf_code);
  801. if( dtmflen >= codelen )
  802. {
  803. strcpy(dtmf_tmp, DTMF_STRING + dtmflen - codelen);
  804. if( strcmp(dtmf_tmp, dtmf_code) == 0 ){
  805. relayControl(GPIO_RELAY, GPIO_ON);
  806. if(DEBUG)
  807. printf( "GPIO_RELAY set on\n" );
  808. //gpio触发后判断是否为延时复位
  809. GetCmdValue(SPK_CONF, "relay_ctrl:mode", relay_mode, sizeof(relay_mode));
  810. if( strcmp(relay_mode, "delay") == 0 )
  811. {
  812. int ret_thrd_delay;
  813. pthread_t thread_delay;
  814. ret_thrd_delay = pthread_create(&thread_delay, NULL, relayDelayControl, NULL);
  815. if (ret_thrd_delay != 0) {
  816. printf("create thread_delay relayDelayControl failed\n");
  817. return;
  818. }
  819. pthread_detach(thread_delay);
  820. }
  821. reset = TRUE;
  822. }
  823. }
  824. }
  825. if(strcmp(srceen_dtmf_event, "yes") == 0 )
  826. {
  827. GetCmdValue(SPK_CONF, "screen_ctrl:dtmf_code", srceen_dtmf_code, sizeof(srceen_dtmf_code));
  828. srceen_codelen = strlen(srceen_dtmf_code);
  829. if( dtmflen >= srceen_codelen )
  830. {
  831. strcpy(dtmf_tmp, DTMF_STRING + dtmflen - srceen_codelen);
  832. if( strcmp(dtmf_tmp, srceen_dtmf_code) == 0 ){
  833. screenControl(TRUE);
  834. if(DEBUG)
  835. printf( "screen set on\n" );
  836. //触发后判断是否为延时复位
  837. GetCmdValue(SPK_CONF, "screen_ctrl:mode", srceen_mode, sizeof(srceen_mode));
  838. if( strcmp(srceen_mode, "delay") == 0 )
  839. {
  840. int ret_thrd_delay;
  841. pthread_t thread_delay;
  842. ret_thrd_delay = pthread_create(&thread_delay, NULL, screenDelayControl, NULL);
  843. if (ret_thrd_delay != 0) {
  844. printf("create thread_delay screenDelayControl failed\n");
  845. return;
  846. }
  847. pthread_detach(thread_delay);
  848. }
  849. reset = TRUE;
  850. }
  851. }
  852. }
  853. if(reset) memset(DTMF_STRING, 0, sizeof(DTMF_STRING));
  854. }
  855. }
  856. //baresip状态监控
  857. void ProcessReply( redisReply * pReply )
  858. {
  859. cJSON *pJson;
  860. redisReply * pSubReply = NULL;
  861. char callerid[128],strtmp[256];
  862. if ( pReply != NULL && pReply->elements == 3 )
  863. {
  864. pSubReply = pReply->element[2];
  865. if(DEBUG)
  866. printf( "Msg [%s]\n", pSubReply->str );
  867. pJson = cJSON_Parse(pSubReply->str);
  868. if ( pJson ) {
  869. if(strcmp(cJSON_GetObjectItem(pJson, "direction")->valuestring, "incoming") == 0 &&
  870. strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "CALL_ESTABLISHED") == 0)
  871. {
  872. //通话
  873. writeLog( STA, "CALL ESTABLISHED", "incoming" );
  874. //amplifier_switch(TRUE);
  875. BARESIP_STATUS = INUSE;
  876. answeredTrigger();
  877. }
  878. else if(strcmp(cJSON_GetObjectItem(pJson, "direction")->valuestring, "outgoing") == 0 &&
  879. strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "CALL_ESTABLISHED") == 0)
  880. {
  881. //通话
  882. writeLog( STA, "CALL ESTABLISHED", "outgoing" );
  883. //amplifier_switch(TRUE);
  884. BARESIP_STATUS = INUSE;
  885. answeredTrigger();
  886. }
  887. else if(strcmp(cJSON_GetObjectItem(pJson, "direction")->valuestring, "incoming") == 0 &&
  888. strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "CALL_CLOSED") == 0)
  889. {
  890. //挂断
  891. hangupTrigger(cJSON_GetObjectItem(pJson, "param")->valuestring);
  892. writeLog( STA, "CALL CLOSED", cJSON_GetObjectItem(pJson, "param")->valuestring );
  893. if(BARESIP_STATUS != OFFLINE){
  894. BARESIP_STATUS = IDLE;
  895. }
  896. if(strlen(DTMF_STRING))
  897. memset(DTMF_STRING, 0, sizeof(DTMF_STRING));
  898. CURRENT_CALLS --;
  899. if(CURRENT_CALLS < 1)
  900. {
  901. //amplifier_switch(FALSE);
  902. CURRENT_CALLS = 0;
  903. }
  904. else
  905. {
  906. control_cmd(BARESIP_CHAN, "{\"cmd\":\"resume\",\"data\":\"\"}");
  907. }
  908. }
  909. else if(strcmp(cJSON_GetObjectItem(pJson, "direction")->valuestring, "outgoing") == 0 &&
  910. strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "CALL_CLOSED") == 0)
  911. {
  912. //挂断
  913. hangupTrigger(cJSON_GetObjectItem(pJson, "param")->valuestring);
  914. writeLog( STA, "CALL CLOSED", cJSON_GetObjectItem(pJson, "param")->valuestring );
  915. if(BARESIP_STATUS != OFFLINE){
  916. BARESIP_STATUS = IDLE;
  917. }
  918. CURRENT_KEY = UNKEY;
  919. if(strlen(DTMF_STRING))
  920. memset(DTMF_STRING, 0, sizeof(DTMF_STRING));
  921. CURRENT_CALLS --;
  922. if(CURRENT_CALLS < 1)
  923. {
  924. CURRENT_CALLS = 0;
  925. }
  926. else
  927. {
  928. control_cmd(BARESIP_CHAN, "{\"cmd\":\"resume\",\"data\":\"\"}");
  929. }
  930. }
  931. else if(strcmp(cJSON_GetObjectItem(pJson, "direction")->valuestring, "incoming") == 0 &&
  932. strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "CALL_INCOMING") == 0)
  933. {
  934. //呼入振铃状态
  935. writeLog( STA, "INCOMING CALL", cJSON_GetObjectItem(pJson, "peeruri")->valuestring );
  936. CURRENT_CALLS ++;
  937. //amplifier_switch(TRUE);
  938. if(BARESIP_STATUS == IDLE)
  939. {
  940. BARESIP_STATUS = RING;
  941. CURRENT_KEY = UNKEY;
  942. incomingTrigger();
  943. }
  944. memset(callerid, 0, sizeof(callerid));
  945. if(strchr(cJSON_GetObjectItem(pJson, "peeruri")->valuestring,'@'))
  946. strncpy(callerid, cJSON_GetObjectItem(pJson, "peeruri")->valuestring,
  947. strchr(cJSON_GetObjectItem(pJson, "peeruri")->valuestring,'@') - cJSON_GetObjectItem(pJson, "peeruri")->valuestring);
  948. else
  949. strcpy(callerid, cJSON_GetObjectItem(pJson, "peeruri")->valuestring);
  950. setIncomingCallInfo(cJSON_GetObjectItem(pJson, "accountaor")->valuestring, callerid + 4);
  951. }
  952. else if(strcmp(cJSON_GetObjectItem(pJson, "direction")->valuestring, "outgoing") == 0 &&
  953. strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "CALL_OUTGOING") == 0)
  954. {
  955. //呼出回铃状态
  956. writeLog( STA, "OUTGOING CALL", cJSON_GetObjectItem(pJson, "peeruri")->valuestring );
  957. CURRENT_CALLS ++;
  958. //amplifier_switch(TRUE);
  959. if(BARESIP_STATUS == IDLE || BARESIP_STATUS == DIALING)
  960. {
  961. BARESIP_STATUS = RINGING;
  962. outgoingTrigger();
  963. }
  964. memset(callerid, 0, sizeof(callerid));
  965. if(strchr(cJSON_GetObjectItem(pJson, "peeruri")->valuestring,'@'))
  966. strncpy(callerid, cJSON_GetObjectItem(pJson, "peeruri")->valuestring,
  967. strchr(cJSON_GetObjectItem(pJson, "peeruri")->valuestring,'@') - cJSON_GetObjectItem(pJson, "peeruri")->valuestring);
  968. else
  969. strncpy(callerid, cJSON_GetObjectItem(pJson, "peeruri")->valuestring,
  970. strchr(cJSON_GetObjectItem(pJson, "peeruri")->valuestring,';') - cJSON_GetObjectItem(pJson, "peeruri")->valuestring);
  971. setOutgoingCallInfo(cJSON_GetObjectItem(pJson, "accountaor")->valuestring, callerid + 4);
  972. }
  973. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "CALL_DTMF_START") == 0)
  974. {
  975. //接收DTMF
  976. writeLog( FUN, "RECEIVE DTMF", cJSON_GetObjectItem(pJson, "param")->valuestring );
  977. dtmfDetect(cJSON_GetObjectItem(pJson, "param")->valuestring);
  978. }
  979. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "REGISTER_OK") == 0)
  980. {
  981. //如果是注册成功点亮指示灯
  982. char enac1[8],enac2[8],enac3[8],accountaor1[192], accountaor2[192], accountaor3[192];
  983. char account1[32], domain1[128], account2[32], domain2[128], account3[32], domain3[128];
  984. if(BARESIP_STATUS == OFFLINE || BARESIP_STATUS == DIALING || strcmp(cJSON_GetObjectItem(pJson, "class")->valuestring, "init register") == 0)
  985. {
  986. BARESIP_STATUS = IDLE;
  987. CURRENT_CALLS = 0;
  988. }
  989. if(cJSON_GetObjectItem(pJson, "accountaor") != NULL)
  990. {
  991. GetCmdValue(SPK_CONF, "account_info_1:enable", enac1, sizeof(enac1));
  992. if(strcmp(enac1, "yes") == 0 && ACCOUNT1_REG == REGISTER_FAIL)
  993. {
  994. GetCmdValue(SPK_CONF, "account_info_1:username", account1, sizeof(account1));
  995. GetCmdValue(SPK_CONF, "account_info_1:domain", domain1, sizeof(domain1));
  996. if(strlen(domain1) == 0)
  997. {
  998. GetCmdValue(SPK_CONF, "account_info_1:server", domain1, sizeof(domain1));
  999. }
  1000. memset(accountaor1, '\0', sizeof(accountaor1));
  1001. sprintf(accountaor1, "sip:%s@%s", account1, domain1);
  1002. if (strcmp(cJSON_GetObjectItem(pJson, "accountaor")->valuestring, accountaor1) == 0)
  1003. {
  1004. ACCOUNT1_REG = REGISTER_OK;
  1005. cJSON_Delete(pJson);
  1006. sprintf(strtmp,"Primary SIP Account <%s>",accountaor1);
  1007. writeLog( STA, "SIP REGISTERED", strtmp );
  1008. return;
  1009. }
  1010. }
  1011. GetCmdValue(SPK_CONF, "account_info_2:enable", enac2, sizeof(enac2));
  1012. if(strcmp(enac2, "yes") == 0 && ACCOUNT2_REG == REGISTER_FAIL)
  1013. {
  1014. GetCmdValue(SPK_CONF, "account_info_2:username", account2, sizeof(account2));
  1015. GetCmdValue(SPK_CONF, "account_info_2:domain", domain2, sizeof(domain2));
  1016. if(strlen(domain2) == 0)
  1017. {
  1018. GetCmdValue(SPK_CONF, "account_info_2:server", domain2, sizeof(domain2));
  1019. }
  1020. memset(accountaor2, '\0', sizeof(accountaor2));
  1021. sprintf(accountaor2, "sip:%s@%s", account2, domain2);
  1022. if (strcmp(cJSON_GetObjectItem(pJson, "accountaor")->valuestring, accountaor2) == 0)
  1023. {
  1024. ACCOUNT2_REG = REGISTER_OK;
  1025. cJSON_Delete(pJson);
  1026. sprintf(strtmp,"Secondary SIP Account-1 <%s>",accountaor2);
  1027. writeLog( STA, "SIP REGISTERED", strtmp );
  1028. return;
  1029. }
  1030. }
  1031. GetCmdValue(SPK_CONF, "account_info_3:enable", enac3, sizeof(enac3));
  1032. if(strcmp(enac3, "yes") == 0 && ACCOUNT3_REG == REGISTER_FAIL)
  1033. {
  1034. GetCmdValue(SPK_CONF, "account_info_3:username", account3, sizeof(account3));
  1035. GetCmdValue(SPK_CONF, "account_info_3:domain", domain3, sizeof(domain3));
  1036. if(strlen(domain3) == 0)
  1037. {
  1038. GetCmdValue(SPK_CONF, "account_info_3:server", domain3, sizeof(domain3));
  1039. }
  1040. memset(accountaor3, '\0', sizeof(accountaor3));
  1041. sprintf(accountaor3, "sip:%s@%s", account3, domain3);
  1042. if (strcmp(cJSON_GetObjectItem(pJson, "accountaor")->valuestring, accountaor3) == 0)
  1043. {
  1044. ACCOUNT3_REG = REGISTER_OK;
  1045. cJSON_Delete(pJson);
  1046. sprintf(strtmp,"Secondary SIP Account-2 <%s>",accountaor3);
  1047. writeLog( STA, "SIP REGISTERED", strtmp );
  1048. return;
  1049. }
  1050. }
  1051. }
  1052. regcount ++;
  1053. }
  1054. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "REGISTER_FAIL") == 0 )
  1055. {
  1056. char enac1[8],enac2[8],enac3[8],accountaor1[192], accountaor2[192], accountaor3[192];
  1057. char account1[32], domain1[128], account2[32], domain2[128], account3[32], domain3[128];
  1058. if(cJSON_GetObjectItem(pJson, "accountaor") != NULL)
  1059. {
  1060. GetCmdValue(SPK_CONF, "account_info_1:enable", enac1, sizeof(enac1));
  1061. if(strcmp(enac1, "yes") == 0 && ACCOUNT1_REG == REGISTER_OK)
  1062. {
  1063. GetCmdValue(SPK_CONF, "account_info_1:username", account1, sizeof(account1));
  1064. GetCmdValue(SPK_CONF, "account_info_1:domain", domain1, sizeof(domain1));
  1065. if(strlen(domain1) == 0)
  1066. {
  1067. GetCmdValue(SPK_CONF, "account_info_1:server", domain1, sizeof(domain1));
  1068. }
  1069. memset(accountaor1, '\0', sizeof(accountaor1));
  1070. sprintf(accountaor1, "sip:%s@%s", account1, domain1);
  1071. if (strcmp(cJSON_GetObjectItem(pJson, "accountaor")->valuestring, accountaor1) == 0)
  1072. {
  1073. ACCOUNT1_REG = REGISTER_FAIL;
  1074. if( ACCOUNT2_REG == REGISTER_FAIL && ACCOUNT3_REG == REGISTER_FAIL && ACCOUNTP2P_REG == REGISTER_FAIL ){
  1075. BARESIP_STATUS = OFFLINE;
  1076. }
  1077. sprintf(strtmp,"Primary Account REGISTER FAILED <%s>",accountaor1);
  1078. writeLog( STA, strtmp, cJSON_GetObjectItem(pJson, "param")->valuestring );
  1079. cJSON_Delete(pJson);
  1080. return;
  1081. }
  1082. }
  1083. GetCmdValue(SPK_CONF, "account_info_2:enable", enac2, sizeof(enac2));
  1084. if(strcmp(enac2, "yes") == 0 && ACCOUNT2_REG == REGISTER_OK)
  1085. {
  1086. GetCmdValue(SPK_CONF, "account_info_2:username", account2, sizeof(account2));
  1087. GetCmdValue(SPK_CONF, "account_info_2:domain", domain2, sizeof(domain2));
  1088. if(strlen(domain2) == 0)
  1089. {
  1090. GetCmdValue(SPK_CONF, "account_info_2:server", domain2, sizeof(domain2));
  1091. }
  1092. memset(accountaor2, '\0', sizeof(accountaor2));
  1093. sprintf(accountaor2, "sip:%s@%s", account2, domain2);
  1094. if (strcmp(cJSON_GetObjectItem(pJson, "accountaor")->valuestring, accountaor2) == 0)
  1095. {
  1096. ACCOUNT2_REG = REGISTER_FAIL;
  1097. if( ACCOUNT1_REG == REGISTER_FAIL && ACCOUNT3_REG == REGISTER_FAIL && ACCOUNTP2P_REG == REGISTER_FAIL ){
  1098. BARESIP_STATUS = OFFLINE;
  1099. }
  1100. sprintf(strtmp,"Secondary Account-1 REGISTER FAILED <%s>",accountaor2);
  1101. writeLog( STA, strtmp, cJSON_GetObjectItem(pJson, "param")->valuestring );
  1102. cJSON_Delete(pJson);
  1103. return;
  1104. }
  1105. }
  1106. GetCmdValue(SPK_CONF, "account_info_3:enable", enac3, sizeof(enac3));
  1107. if(strcmp(enac3, "yes") == 0 && ACCOUNT3_REG == REGISTER_OK)
  1108. {
  1109. GetCmdValue(SPK_CONF, "account_info_3:username", account3, sizeof(account3));
  1110. GetCmdValue(SPK_CONF, "account_info_3:domain", domain3, sizeof(domain3));
  1111. if(strlen(domain3) == 0)
  1112. {
  1113. GetCmdValue(SPK_CONF, "account_info_3:server", domain3, sizeof(domain3));
  1114. }
  1115. memset(accountaor3, '\0', sizeof(accountaor3));
  1116. sprintf(accountaor3, "sip:%s@%s", account3, domain3);
  1117. if (strcmp(cJSON_GetObjectItem(pJson, "accountaor")->valuestring, accountaor3) == 0)
  1118. {
  1119. ACCOUNT3_REG = REGISTER_FAIL;
  1120. if( ACCOUNT1_REG == REGISTER_FAIL && ACCOUNT2_REG == REGISTER_FAIL && ACCOUNTP2P_REG == REGISTER_FAIL ){
  1121. BARESIP_STATUS = OFFLINE;
  1122. }
  1123. sprintf(strtmp,"Secondary Account-2 REGISTER FAILED <%s>",accountaor3);
  1124. writeLog( STA, strtmp, cJSON_GetObjectItem(pJson, "param")->valuestring );
  1125. cJSON_Delete(pJson);
  1126. return;
  1127. }
  1128. }
  1129. }
  1130. regcount ++;
  1131. }
  1132. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "EXIT") == 0)
  1133. {
  1134. //如果是注册失败熄灭指示灯
  1135. BARESIP_STATUS = OFFLINE;
  1136. ACCOUNT1_REG = REGISTER_FAIL;
  1137. ACCOUNT2_REG = REGISTER_FAIL;
  1138. ACCOUNT3_REG = REGISTER_FAIL;
  1139. writeLog( STA, "APP EXIT", "" );
  1140. CURRENT_CALLS = 0;
  1141. }
  1142. else if(cJSON_GetObjectItem(pJson, "event")->type == 1 && cJSON_GetObjectItem(pJson, "event")->valueint == 0){
  1143. if(strncmp(cJSON_GetObjectItem(pJson, "data")->valuestring,"\n--- User Agents",16) == 0)
  1144. {
  1145. char p2penable[8];
  1146. GetCmdValue(SPK_CONF, "account_info_p2p:enable", p2penable, sizeof(p2penable));
  1147. if(strcmp(p2penable,"yes") == 0)
  1148. {
  1149. BARESIP_STATUS = IDLE;
  1150. if(ACCOUNTP2P_REG == REGISTER_FAIL)
  1151. {
  1152. ACCOUNTP2P_REG = REGISTER_OK;
  1153. writeLog( STA, "P2P ACCOUNT", "Enabled" );
  1154. }
  1155. }
  1156. else if(ACCOUNTP2P_REG == REGISTER_OK)
  1157. {
  1158. ACCOUNTP2P_REG = REGISTER_FAIL;
  1159. writeLog( STA, "P2P ACCOUNT", "Disabled" );
  1160. }
  1161. }
  1162. }
  1163. if(DEBUG)
  1164. printf( "Baresip Status is: %d\n", BARESIP_STATUS );
  1165. }else{
  1166. printf( "parse failed!\n");
  1167. }
  1168. if(pJson != NULL) cJSON_Delete(pJson);
  1169. }
  1170. }
  1171. /*
  1172. * 订阅baresip实时状态.
  1173. */
  1174. void *baresip_status()
  1175. {
  1176. redisContext * pContext = redisConnectUnix(unix_socket_path);
  1177. if ( NULL == pContext || pContext->err == 1 )
  1178. {
  1179. printf( "%s\n", pContext->errstr );
  1180. exit( -1 );
  1181. }
  1182. char * pKey = "session-channel";
  1183. redisReply * pReply = redisCommand( pContext, "SUBSCRIBE %s", pKey );
  1184. freeReplyObject( pReply );
  1185. while ( redisGetReply( pContext, (void **)&pReply ) == REDIS_OK )
  1186. {
  1187. ProcessReply( pReply );
  1188. freeReplyObject( pReply );
  1189. }
  1190. redisFree( pContext );
  1191. }
  1192. int get_line(int * line_id)
  1193. {
  1194. char enac[8], strtmp[32];
  1195. int id = 0;
  1196. memset(line_id,-1,sizeof(line_id));
  1197. for(int i = 1; i < 4;i ++)
  1198. {
  1199. bzero(strtmp, sizeof(strtmp));
  1200. sprintf(strtmp,"account_info_%d:enable",i);
  1201. GetCmdValue(SPK_CONF, strtmp, enac, sizeof(enac));
  1202. if(strcmp(enac,"yes") == 0)
  1203. {
  1204. line_id[i] = id;
  1205. id ++;
  1206. }
  1207. }
  1208. return TRUE;
  1209. }
  1210. //按键动作
  1211. void keyAction(int key)
  1212. {
  1213. char cmd[MAX_PIPE_BUFSIZE];
  1214. char accountaor[192],strtmp[64];
  1215. char account[32], enable[8],url[256],ipaddr[16],macaddr[16];
  1216. char onekey_num[32],onekey_line[24],repress_cancel[8];
  1217. int channel = BARESIP_CHAN, line_id[4];
  1218. if(DEBUG)
  1219. printf( "BARESIP_STATUS [%d]\n", BARESIP_STATUS );
  1220. switch(key){
  1221. case KEY://处理按键动作
  1222. if( CURRENT_KEY == UNKEY || CURRENT_KEY == KEY )
  1223. {
  1224. switch(BARESIP_STATUS)
  1225. {
  1226. case IDLE:
  1227. CURRENT_KEY = KEY;
  1228. BARESIP_STATUS = DIALING;
  1229. GetCmdValue(SPK_CONF, "intercom:onekey_num", onekey_num, sizeof(onekey_num));
  1230. GetCmdValue(SPK_CONF, "intercom:onekey_line", onekey_line, sizeof(onekey_line));
  1231. if(strlen(onekey_num) == 0)
  1232. {
  1233. BARESIP_STATUS = IDLE;
  1234. break;
  1235. }
  1236. get_line(line_id);
  1237. if(strcmp(onekey_line,"auto") == 0)
  1238. {
  1239. if(DEBUG)
  1240. printf("ACCOUNT1 is %d, ACCOUNT2 is %d, ACCOUNT3 is %d\n",ACCOUNT1_REG,ACCOUNT2_REG,ACCOUNT3_REG);
  1241. if(ACCOUNT1_REG == REGISTER_OK)
  1242. {
  1243. if(line_id[1] == -1)
  1244. {
  1245. BARESIP_STATUS = IDLE;
  1246. break;
  1247. }
  1248. sprintf(cmd,"{\"cmd\":\"dial\",\"data\":\"%s %d\"}",onekey_num, line_id[1]);
  1249. }
  1250. else if(ACCOUNT2_REG == REGISTER_OK)
  1251. {
  1252. if(line_id[2] == -1)
  1253. {
  1254. BARESIP_STATUS = IDLE;
  1255. break;
  1256. }
  1257. sprintf(cmd,"{\"cmd\":\"dial\",\"data\":\"%s %d\"}",onekey_num, line_id[2]);
  1258. }
  1259. else if(ACCOUNT3_REG == REGISTER_OK)
  1260. {
  1261. if(line_id[3] == -1)
  1262. {
  1263. BARESIP_STATUS = IDLE;
  1264. break;
  1265. }
  1266. sprintf(cmd,"{\"cmd\":\"dial\",\"data\":\"%s %d\"}",onekey_num, line_id[3]);
  1267. }
  1268. else
  1269. {
  1270. BARESIP_STATUS = IDLE;
  1271. break;
  1272. }
  1273. }
  1274. else if (strcmp(onekey_line, "account_info_1") == 0)
  1275. {
  1276. if(line_id[1] == -1 || ACCOUNT1_REG != REGISTER_OK)
  1277. {
  1278. BARESIP_STATUS = IDLE;
  1279. break;
  1280. }
  1281. sprintf(cmd,"{\"cmd\":\"dial\",\"data\":\"%s %d\"}",onekey_num, line_id[1]);
  1282. }
  1283. else if (strcmp(onekey_line, "account_info_2") == 0)
  1284. {
  1285. if(line_id[2] == -1 || ACCOUNT2_REG != REGISTER_OK)
  1286. {
  1287. BARESIP_STATUS = IDLE;
  1288. break;
  1289. }
  1290. sprintf(cmd,"{\"cmd\":\"dial\",\"data\":\"%s %d\"}",onekey_num, line_id[2]);
  1291. }
  1292. else if (strcmp(onekey_line, "account_info_3") == 0)
  1293. {
  1294. if(line_id[3] == -1 || ACCOUNT3_REG != REGISTER_OK)
  1295. {
  1296. BARESIP_STATUS = IDLE;
  1297. break;
  1298. }
  1299. sprintf(cmd,"{\"cmd\":\"dial\",\"data\":\"%s %d\"}",onekey_num, line_id[3]);
  1300. }
  1301. else if(strcmp(onekey_line,"p2p") == 0)
  1302. {
  1303. sprintf(cmd,"{\"cmd\":\"dial\",\"data\":\"%s\"}",onekey_num);
  1304. }
  1305. if(DEBUG)
  1306. printf( "dial [%s]\n", cmd );
  1307. control_cmd(channel, cmd);
  1308. sprintf(strtmp, "%s <%s>", onekey_num, onekey_line);
  1309. writeLog( FUN, "KEY Call", strtmp );
  1310. break;
  1311. case RINGING:
  1312. case INUSE:
  1313. GetCmdValue(SPK_CONF, "intercom:repress_cancel", repress_cancel, sizeof(repress_cancel));
  1314. if(strcmp(repress_cancel, "yes") == 0){
  1315. strcpy(cmd,"{\"cmd\":\"hangup\",\"data\":\"\"}");
  1316. if(DEBUG)
  1317. printf( "hangup call\n" );
  1318. control_cmd(channel, cmd);
  1319. writeLog( FUN, "KEY Hangup", "" );
  1320. }
  1321. break;
  1322. case RING:
  1323. CURRENT_KEY = KEY;
  1324. strcpy(cmd,"{\"cmd\":\"accept\",\"data\":\"\"}");
  1325. if(DEBUG)
  1326. printf( "answer call\n" );
  1327. control_cmd(channel, cmd);
  1328. writeLog( FUN, "KEY Answer", "" );
  1329. break;
  1330. case OFFLINE:
  1331. GetCmdValue(SPK_CONF, "call_action_url:unregistered_enable", enable, sizeof(enable));
  1332. if(strcmp(enable,"yes") == 0)
  1333. {
  1334. GetCmdValue(SPK_CONF, "call_action_url:unregistered_url", url, sizeof(url));
  1335. if(strstr(url,VAR_IP))
  1336. {
  1337. if(get_ip(ipaddr))
  1338. strcpy(url, strrpl(url, VAR_IP, ipaddr));
  1339. }
  1340. if(strstr(url,VAR_MAC))
  1341. {
  1342. if(get_mac(macaddr))
  1343. strcpy(url, strrpl(url, VAR_MAC, macaddr));
  1344. }
  1345. if(strstr(url,VAR_UA))
  1346. {
  1347. GetCmdValue(SPK_CONF, "intercom:onekey_line", onekey_line, sizeof(onekey_line));
  1348. sprintf(strtmp,"%s:username",onekey_line);
  1349. GetCmdValue(SPK_CONF, strtmp, account, sizeof(account));
  1350. strcpy(url, strrpl(url, VAR_UA, account));
  1351. }
  1352. if(strstr(url,VAR_NUM))
  1353. {
  1354. GetCmdValue(SPK_CONF, "intercom:onekey_num", onekey_num, sizeof(onekey_num));
  1355. strcpy(url, strrpl(url, VAR_NUM, onekey_num));
  1356. }
  1357. action_url(url);
  1358. writeLog(FUN, "HTTP Request", url);
  1359. }
  1360. break;
  1361. }
  1362. }
  1363. break;
  1364. case KEY2_10:
  1365. system("/etc/scripts/play_rebooting.sh");
  1366. system("rm -f /oem/.userdata && /sbin/reboot");
  1367. break;
  1368. case KEY2_3:
  1369. PlayIP();
  1370. break;
  1371. }
  1372. }
  1373. void *checkReg()
  1374. {
  1375. dictionary * ini ;
  1376. ini = iniparser_load(SPK_CONF);
  1377. if (ini==NULL) {
  1378. fprintf(stderr, "cannot parse file: %s\n", SPK_CONF);
  1379. return FALSE;
  1380. }
  1381. if((strcmp(iniparser_getstring(ini, "account_info_1:enable", "no"), "yes") == 0 || strcmp(iniparser_getstring(ini, "account_info_2:enable", "no"), "yes") == 0 ||
  1382. strcmp(iniparser_getstring(ini, "account_info_3:enable", "no"), "yes") == 0) && regcount == 0)
  1383. {
  1384. system("/etc/scrtips/sipphone.sh stop");
  1385. }
  1386. regcount = 0;
  1387. iniparser_freedict(ini);
  1388. }
  1389. void reset_vlcInfo(int id)
  1390. {
  1391. vlcInfo[id].pid = 0;
  1392. vlcInfo[id].prio = 0;
  1393. memset(vlcInfo->param, 0, sizeof(vlcInfo->param));
  1394. }
  1395. int file_exists(char *filename)
  1396. {
  1397. return (access(filename, 0) == 0);
  1398. }
  1399. int vlc_cmd_action(char *action, int id)
  1400. {
  1401. char cmd[128], ctrlfile[32];
  1402. int i = 0;
  1403. sprintf(ctrlfile,"/tmp/vlc_%d.sock",id);
  1404. while(i < MAX_TRY)
  1405. {
  1406. if(file_exists(ctrlfile))
  1407. {
  1408. sprintf(cmd,"echo \"%s\" | socat - unix-connect:%s", action, ctrlfile);
  1409. if(DEBUG) printf("volume CMD: %s\n", cmd);
  1410. if(system(cmd) == 0)
  1411. return TRUE;
  1412. else
  1413. continue;
  1414. }
  1415. usleep(200*1000);
  1416. i ++;
  1417. }
  1418. return FALSE;
  1419. }
  1420. int schedule_start_check_prio(int prio)
  1421. {
  1422. char cmd[64];
  1423. for(int i = 1; i < 31; i++)
  1424. {
  1425. if(DEBUG)
  1426. printf("==============%d,%d,%d,%s=============\n", i, SCHEDULE[i], vlcInfo[i].prio, vlcInfo[i].param);
  1427. if(SCHEDULE[i] != STOP)
  1428. {
  1429. if(prio < vlcInfo[i].prio)
  1430. return FALSE;
  1431. else if(SCHEDULE[i] == RUNNING && prio > vlcInfo[i].prio)
  1432. {
  1433. if(vlc_cmd_action("pause",i))
  1434. SCHEDULE[i] = PAUSED;
  1435. }
  1436. }
  1437. }
  1438. return TRUE;
  1439. }
  1440. void schedule_stop_check_prio(int prio)
  1441. {
  1442. char cmd[64];
  1443. int paused[30],running[30],c_paused = 0, c_running = 0;
  1444. int isRunning = FALSE;
  1445. for(int i = 1; i < 31; i++)
  1446. {
  1447. if(SCHEDULE[i] == RUNNING)
  1448. {
  1449. return;
  1450. }
  1451. if(SCHEDULE[i] == PAUSED)
  1452. {
  1453. paused[c_paused] = i;
  1454. c_paused ++;
  1455. }
  1456. }
  1457. for(int p = prio; p > 0; p--)
  1458. {
  1459. for(int n = 0; n < c_paused; n++)
  1460. {
  1461. if(p == vlcInfo[paused[n]].prio)
  1462. {
  1463. state_event(AUDIO_ON);
  1464. if(vlc_cmd_action("pause",paused[n]))
  1465. {
  1466. SCHEDULE[paused[n]] = RUNNING;
  1467. isRunning = TRUE;
  1468. }
  1469. }
  1470. }
  1471. if(isRunning) return;
  1472. }
  1473. state_event(AUDIO_OFF);
  1474. }
  1475. void playAudio(int id, int times, int isBeep, char *enSrceen, char *file, int prio, int vol)
  1476. {
  1477. char filetmp[64], action[128], sockio[32], paused[32],loop[16],cmd[64], gain[32];
  1478. int status, isPaused;
  1479. bzero(paused,sizeof(paused));
  1480. if(!schedule_start_check_prio(prio))
  1481. {
  1482. strcpy(paused,"--start-paused");
  1483. isPaused = TRUE;
  1484. }
  1485. else
  1486. {
  1487. strcpy(paused,"--no-start-paused");
  1488. isPaused = FALSE;
  1489. state_event(AUDIO_ON);
  1490. }
  1491. if(times == 0)
  1492. {
  1493. sprintf(filetmp, "/usr/share/baresip/%s", file);
  1494. strcpy(loop,"--loop");
  1495. }
  1496. else
  1497. {
  1498. sprintf(filetmp,"/tmp/playlist_%d.m3u",id);
  1499. FILE *fd = fopen(filetmp, "w+");
  1500. if(fd == NULL)
  1501. {
  1502. if(DEBUG) printf("play failed!\n");
  1503. return;
  1504. }
  1505. if(isBeep) fprintf(fd,"/usr/share/baresip/s_beep.mp3\n");
  1506. for(int i=0;i<times;i++)
  1507. {
  1508. fprintf(fd,"/usr/share/baresip/%s\n",file);
  1509. }
  1510. if(isBeep) fprintf(fd,"/usr/share/baresip/e_beep.mp3\n");
  1511. fclose(fd);
  1512. strcpy(loop,"--no-loop");
  1513. }
  1514. sprintf(gain, "--gain=%.02f", (float) vol/100);
  1515. sprintf(sockio,"--rc-unix=/tmp/vlc_%d.sock", id);
  1516. char *argv[] = {"vlc","--play-and-exit","--control","rc","--rc-fake-tty",sockio,paused,loop,gain,filetmp,NULL};
  1517. status = posix_spawn(&vlcInfo[id].pid,"/usr/sbin/vlc",NULL,NULL,argv,NULL);
  1518. if(status == 0)
  1519. {
  1520. usleep(300*1000);
  1521. // sprintf(action,"volume %d", 256 * vol/100);
  1522. // vlc_cmd_action(action,id);
  1523. SCHEDULE[id] = (isPaused)?PAUSED:RUNNING;
  1524. vlcInfo[id].prio = prio;
  1525. sprintf(vlcInfo[id].param,"%s", file);
  1526. sprintf(action,"Play audio: %s", vlcInfo[id].param);
  1527. writeLog(FUN, "Audio Control", action);
  1528. //system("echo 0 > /sys/class/gpio/gpio5/value");
  1529. if(waitpid(vlcInfo[id].pid, &status, 0) != -1)
  1530. {
  1531. if(DEBUG) printf("Child exited with status %i\n", status);
  1532. SCHEDULE[id] = STOP;
  1533. schedule_stop_check_prio(vlcInfo[id].prio);
  1534. reset_vlcInfo(id);
  1535. if(strcmp(enSrceen, "Disabled") != 0)
  1536. {
  1537. sprintf(cmd,"{\"action\": \"off\", \"id\": %s}", enSrceen);
  1538. screen_control(cmd);
  1539. }
  1540. //system("echo 1 > /sys/class/gpio/gpio5/value");
  1541. sprintf(action,"Stop audio: %s", vlcInfo[id].param);
  1542. writeLog(FUN, "Audio Control", action);
  1543. }
  1544. }
  1545. if(times) remove(filetmp);
  1546. }
  1547. void *handle_playAudio(void *arg)
  1548. {
  1549. int selfkey, isBeep = FALSE, times = 0;
  1550. PlayInfo *p = arg;
  1551. selfkey = p->key_id;
  1552. if(strlen(p->times_str)) times = atoi(p->times_str);
  1553. if(strcmp(p->enBeep, "yes") == 0) isBeep = TRUE;
  1554. if(strlen(p->file))
  1555. {
  1556. playAudio(selfkey, times, isBeep, p->enSrceen, p->file, p->prio, p->vol);
  1557. }
  1558. free(p);
  1559. }
  1560. int equalDate(struct date A,struct date B)
  1561. {
  1562. if(A.year==B.year && A.month==B.month && A.day==B.day)
  1563. return TRUE;
  1564. return FALSE;
  1565. }
  1566. int compareDate(struct date A,struct date B)
  1567. {
  1568. if(A.year<B.year)
  1569. return TRUE;
  1570. if(A.year==B.year && A.month<B.month)
  1571. return TRUE;
  1572. if(A.year==B.year && A.month==B.month && A.day<B.day)
  1573. return TRUE;
  1574. return FALSE;
  1575. }
  1576. void date_format(char *date_str, struct date *s_holiday, struct date *e_holiday)
  1577. {
  1578. struct date date;
  1579. sscanf(date_str, "%d-%d-%d:%d-%d-%d", &s_holiday->year, &s_holiday->month, &s_holiday->day, &e_holiday->year, &e_holiday->month, &e_holiday->day);
  1580. }
  1581. int isHoliday(struct date currDate, char *holidays)
  1582. {
  1583. cJSON *pJson, *pSub;
  1584. struct date s_holiday, e_holiday;
  1585. pJson = cJSON_Parse(holidays);
  1586. if (pJson)
  1587. {
  1588. cJSON *holidaysArray = cJSON_GetObjectItem( pJson, "holidays");
  1589. if(holidaysArray != NULL)
  1590. {
  1591. int array_size = cJSON_GetArraySize (holidaysArray);
  1592. for(int n = 0; n < array_size; n++)
  1593. {
  1594. pSub = cJSON_GetArrayItem(holidaysArray, n);
  1595. date_format(pSub->valuestring, &s_holiday, &e_holiday);
  1596. if(DEBUG)
  1597. {
  1598. printf("current date: %d-%d-%d\n",currDate.year, currDate.month, currDate.day);
  1599. printf("holiday start: %d-%d-%d\n",s_holiday.year, s_holiday.month, s_holiday.day);
  1600. printf("holiday end: %d-%d-%d\n",e_holiday.year, e_holiday.month, e_holiday.day);
  1601. }
  1602. if(equalDate(currDate, s_holiday) || (compareDate(s_holiday, currDate) && compareDate(currDate, e_holiday)))
  1603. {
  1604. cJSON_Delete(pJson);
  1605. return TRUE;
  1606. }
  1607. }
  1608. }
  1609. }
  1610. cJSON_Delete(pJson);
  1611. return FALSE;
  1612. }
  1613. void schedule(int year, int month, int day, int hour, int minute, int week)
  1614. {
  1615. char actiontype[32], keyaction[16], keyactiontype[32], type[16], action_type[16];
  1616. char keystr[32],enable[8],weeks[8], enholiday[8], holidays[4096];
  1617. char w[4],action[128],screen_id[16];
  1618. int s_hour, e_hour, s_min, e_min;
  1619. int s_time, e_time, time, interval;
  1620. struct date s_date, e_date, date;
  1621. dictionary * ini ;
  1622. date.year = year;
  1623. date.month = month;
  1624. date.day = day;
  1625. ini = iniparser_load(SCHEDULE_CONF);
  1626. if (ini==NULL) {
  1627. fprintf(stderr, "cannot parse file: %s\n", SCHEDULE_CONF);
  1628. return;
  1629. }
  1630. strcpy(holidays, iniparser_getstring(ini, "holidays:data", "{\"holidays\": []}"));
  1631. for(int i=1;i<31;i++)
  1632. {
  1633. sprintf(keystr,"schedule_%d:enable",i);
  1634. strcpy(enable, iniparser_getstring(ini, keystr, "no"));
  1635. if(strcmp(enable,"yes") == 0)
  1636. {
  1637. sprintf(keystr,"schedule_%d:enholiday",i);
  1638. strcpy(enholiday, iniparser_getstring(ini, keystr, "no"));
  1639. if(strcmp(enholiday, "yes") == 0 && isHoliday(date, holidays)) continue;
  1640. sprintf(keystr,"schedule_%d:weeks",i);
  1641. strcpy(weeks, iniparser_getstring(ini, keystr, ""));
  1642. sprintf(w,"%d",week);
  1643. if(strchr(weeks,w[0]) == NULL) continue;
  1644. sprintf(keystr,"schedule_%d:s_year",i);
  1645. s_date.year = iniparser_getint(ini, keystr, 0);
  1646. sprintf(keystr,"schedule_%d:e_year",i);
  1647. e_date.year = iniparser_getint(ini, keystr, 0);
  1648. sprintf(keystr,"schedule_%d:s_month",i);
  1649. s_date.month = iniparser_getint(ini, keystr, 0);
  1650. sprintf(keystr,"schedule_%d:e_month",i);
  1651. e_date.month = iniparser_getint(ini, keystr, 0);
  1652. sprintf(keystr,"schedule_%d:s_day",i);
  1653. s_date.day = iniparser_getint(ini, keystr, 0);
  1654. sprintf(keystr,"schedule_%d:e_day",i);
  1655. e_date.day = iniparser_getint(ini, keystr, 0);
  1656. if(compareDate(date, s_date) || compareDate(e_date, date)) continue;
  1657. sprintf(keystr,"schedule_%d:s_hour",i);
  1658. s_hour = iniparser_getint(ini, keystr, 0);
  1659. sprintf(keystr,"schedule_%d:e_hour",i);
  1660. e_hour = iniparser_getint(ini, keystr, 0);
  1661. sprintf(keystr,"schedule_%d:s_min",i);
  1662. s_min = iniparser_getint(ini, keystr, 0);
  1663. sprintf(keystr,"schedule_%d:e_min",i);
  1664. e_min = iniparser_getint(ini, keystr, 0);
  1665. sprintf(keystr,"schedule_%d:interval",i);
  1666. interval = iniparser_getint(ini, keystr, 60);
  1667. sprintf(keystr,"schedule_%d:trigger_screen",i);
  1668. strcpy(screen_id, iniparser_getstring(ini, keystr, "Disabled"));
  1669. time = hour * 60 + minute;
  1670. s_time = s_hour * 60 + s_min;
  1671. e_time = e_hour * 60 + e_min;
  1672. if(time >= s_time && time <= e_time)
  1673. {
  1674. if(time == e_time)
  1675. {
  1676. sprintf(keystr,"%d",i);
  1677. writeLog(SCHED, keystr, "Executed");
  1678. if(SCHEDULE[i] == RUNNING)
  1679. {
  1680. char strtmp[64],cmd[128];
  1681. if(strcmp(screen_id, "Disabled") != 0)
  1682. {
  1683. sprintf(strtmp,"{\"action\": \"off\", \"id\": %s}", screen_id);
  1684. screen_control(strtmp);
  1685. }
  1686. sprintf(cmd,"kill -9 %d",vlcInfo[i].pid);
  1687. system(cmd);
  1688. // system("echo 1 > /sys/class/gpio/gpio5/value");
  1689. // SCHEDULE[i] = STOP;
  1690. // sprintf(action,"Stop audio: %s", vlcInfo[i].param);
  1691. // writeLog(FUN, "Audio Control", action);
  1692. }
  1693. continue;
  1694. }
  1695. if((interval != 0 && (time - s_time) % interval == 0) || (interval == 0 && time == s_time))
  1696. {
  1697. sprintf(keystr,"%d",i);
  1698. writeLog(SCHED, keystr, "Executed");
  1699. char cmd[256], strtmp[64], source[16];
  1700. pthread_t thread_audio;
  1701. int ret_thrd;
  1702. if(SCHEDULE[i] == STOP)
  1703. {
  1704. PlayInfo *playAudioInfo = malloc(sizeof(PlayInfo));
  1705. playAudioInfo->key_id = i;
  1706. bzero(strtmp, sizeof(strtmp));
  1707. sprintf(strtmp,"schedule_%d:source",i);
  1708. strcpy(source, iniparser_getstring(ini, strtmp, ""));
  1709. bzero(strtmp, sizeof(strtmp));
  1710. sprintf(strtmp, "schedule_%d:audio_file", i);
  1711. strcpy(playAudioInfo->file, iniparser_getstring(ini, strtmp, ""));
  1712. bzero(strtmp, sizeof(strtmp));
  1713. sprintf(strtmp,"schedule_%d:play_times",i);
  1714. strcpy(playAudioInfo->times_str, iniparser_getstring(ini, strtmp, ""));
  1715. bzero(strtmp, sizeof(strtmp));
  1716. sprintf(strtmp,"schedule_%d:enBeep",i);
  1717. strcpy(playAudioInfo->enBeep, iniparser_getstring(ini, strtmp, "no"));
  1718. strcpy(playAudioInfo->enSrceen, screen_id);
  1719. bzero(strtmp, sizeof(strtmp));
  1720. sprintf(strtmp,"schedule_%d:priority",i);
  1721. playAudioInfo->prio = iniparser_getint(ini, strtmp, 1);
  1722. bzero(strtmp, sizeof(strtmp));
  1723. sprintf(strtmp,"schedule_%d:volume",i);
  1724. playAudioInfo->vol = iniparser_getint(ini, strtmp, 100);
  1725. if(strcmp(screen_id, "Disabled") != 0)
  1726. {
  1727. sprintf(strtmp,"{\"action\": \"on\", \"id\": %s}", screen_id);
  1728. screen_control(strtmp);
  1729. }
  1730. ret_thrd = pthread_create(&thread_audio, NULL, handle_playAudio, playAudioInfo);
  1731. if (ret_thrd != 0)
  1732. {
  1733. printf("create thread1 handle_playAudio failed\n");
  1734. free(playAudioInfo);
  1735. continue;
  1736. }
  1737. pthread_detach(thread_audio);
  1738. }
  1739. else
  1740. {
  1741. if(strcmp(screen_id, "Disabled") != 0)
  1742. {
  1743. sprintf(strtmp,"{\"action\": \"off\", \"id\": %s}", screen_id);
  1744. screen_control(strtmp);
  1745. }
  1746. sprintf(cmd,"kill -9 %d",vlcInfo[i].pid);
  1747. system(cmd);
  1748. // system("echo 1 > /sys/class/gpio/gpio5/value");
  1749. // SCHEDULE[i] = STOP;
  1750. // sprintf(action,"Stop audio: %s", vlcInfo[i].param);
  1751. // writeLog(FUN, "Audio Control", action);
  1752. }
  1753. }
  1754. }
  1755. }
  1756. }
  1757. iniparser_freedict(ini);
  1758. }
  1759. void *handleDateTime()
  1760. {
  1761. time_t nSeconds;
  1762. struct tm *pTM;
  1763. int year, month, hour, count;
  1764. pthread_t thread1;
  1765. while(1)
  1766. {
  1767. time(&nSeconds);
  1768. pTM = localtime(&nSeconds);
  1769. if(pTM->tm_sec == 0)
  1770. {
  1771. // year = pTM->tm_year + 1900;
  1772. // month = pTM->tm_mon + 1;
  1773. // schedule(year, month, pTM->tm_mday, pTM->tm_hour, pTM->tm_min, pTM->tm_wday);
  1774. count ++;
  1775. if(count > 120)
  1776. {
  1777. pthread_create(&thread1, NULL, checkReg, NULL);
  1778. pthread_detach(thread1);
  1779. count = 0;
  1780. }
  1781. }
  1782. sleep(1);
  1783. }
  1784. }
  1785. //解析音量设置数据
  1786. void ProcessVolumeReply( redisReply * pReply )
  1787. {
  1788. cJSON *pJson = NULL;
  1789. redisReply * pSubReply = NULL;
  1790. char cmd[MAX_PIPE_BUFSIZE];
  1791. int channel = BARESIP_CHAN;
  1792. if ( pReply != NULL && pReply->elements == 3 )
  1793. {
  1794. pSubReply = pReply->element[2];
  1795. pJson = cJSON_Parse(pSubReply->str);
  1796. if ( pJson ) {
  1797. if(cJSON_GetObjectItem(pJson, "audio_volume"))
  1798. {
  1799. if(cJSON_GetObjectItem(pJson, "audio_volume")->valueint == 0)
  1800. schedule_start_check_prio(MAX_PRIORITY + 1);
  1801. else
  1802. schedule_stop_check_prio(MAX_PRIORITY + 1);
  1803. }
  1804. }else{
  1805. printf( "parse failed!\n");
  1806. }
  1807. if(pJson != NULL) cJSON_Delete(pJson);
  1808. }
  1809. }
  1810. /*
  1811. * 订阅音量信息.
  1812. */
  1813. void *get_volume_info()
  1814. {
  1815. redisContext * pContext = redisConnectUnix(unix_socket_path);
  1816. if ( NULL == pContext || pContext->err == 1 )
  1817. {
  1818. printf( "%s\n", pContext->errstr );
  1819. exit( -1 );
  1820. }
  1821. char * pKey = "volume-value-channel";
  1822. redisReply * pReply = redisCommand( pContext, "SUBSCRIBE %s", pKey );
  1823. freeReplyObject( pReply );
  1824. while ( redisGetReply( pContext, (void **)&pReply ) == REDIS_OK )
  1825. {
  1826. ProcessVolumeReply( pReply );
  1827. freeReplyObject( pReply );
  1828. }
  1829. redisFree( pContext );
  1830. }
  1831. void play_audio(char *audiofile, int repeat)
  1832. {
  1833. char cmd[320];
  1834. int channel = BARESIP_CHAN;
  1835. sprintf(cmd,"{\"cmd\":\"play\",\"data\":\"%s:%d\"}",audiofile,repeat);
  1836. control_cmd(channel, cmd);
  1837. }
  1838. const int get_sip_status(char *accountaor)
  1839. {
  1840. int status = OFFLINE;
  1841. const int id = redis_pool_get_context(redisClient);
  1842. if (id != -1) {
  1843. redisReply *reply = NULL;
  1844. reply = (redisReply *) redisCommand(redisClient->context[id], "GET %s", accountaor);
  1845. if (reply && reply->type == REDIS_REPLY_STRING) {
  1846. // 处理回复
  1847. if(DEBUG) printf("SIP acc: %s, status: %s\n", accountaor, reply->str);
  1848. if(strcmp(reply->str, "{\"status\":\"idle\"}") == 0)
  1849. {
  1850. status = IDLE;
  1851. }
  1852. else if(strcmp(reply->str, "{\"status\":\"answer\"}") == 0)
  1853. {
  1854. status = INUSE;
  1855. }
  1856. else if(strcmp(reply->str, "{\"status\":\"ringing\"}") == 0)
  1857. {
  1858. status = RINGING;
  1859. }
  1860. else if(strcmp(reply->str, "{\"status\":\"ring\"}") == 0)
  1861. {
  1862. status = RING;
  1863. }
  1864. }
  1865. if(reply != NULL) freeReplyObject(reply);
  1866. redis_pool_release_connection(redisClient, id);
  1867. }
  1868. if(BARESIP_STATUS == OFFLINE || BARESIP_STATUS == IDLE)
  1869. BARESIP_STATUS = status;
  1870. return status;
  1871. }
  1872. void sip_reg_status_init()
  1873. {
  1874. //初始化SIP注册状态
  1875. char enac1[8],enac2[8],enac3[8],p2penable[8],accountaor1[192], accountaor2[192], accountaor3[192];
  1876. char account1[32], domain1[128], account2[32], domain2[128], account3[32], domain3[128];
  1877. GetCmdValue(SPK_CONF, "account_info_1:enable", enac1, sizeof(enac1));
  1878. if(strcmp(enac1, "yes") == 0)
  1879. {
  1880. GetCmdValue(SPK_CONF, "account_info_1:username", account1, sizeof(account1));
  1881. GetCmdValue(SPK_CONF, "account_info_1:domain", domain1, sizeof(domain1));
  1882. if(strlen(domain1) == 0)
  1883. {
  1884. GetCmdValue(SPK_CONF, "account_info_1:server", domain1, sizeof(domain1));
  1885. }
  1886. memset(accountaor1, '\0', sizeof(accountaor1));
  1887. sprintf(accountaor1, "baresip-call-status-%s-%s", account1, domain1);
  1888. if(get_sip_status(accountaor1) != OFFLINE)
  1889. {
  1890. ACCOUNT1_REG = REGISTER_OK;
  1891. }
  1892. }
  1893. GetCmdValue(SPK_CONF, "account_info_2:enable", enac2, sizeof(enac2));
  1894. if(strcmp(enac2, "yes") == 0)
  1895. {
  1896. GetCmdValue(SPK_CONF, "account_info_2:username", account2, sizeof(account2));
  1897. GetCmdValue(SPK_CONF, "account_info_2:domain", domain2, sizeof(domain2));
  1898. if(strlen(domain2) == 0)
  1899. {
  1900. GetCmdValue(SPK_CONF, "account_info_2:server", domain2, sizeof(domain2));
  1901. }
  1902. memset(accountaor2, '\0', sizeof(accountaor2));
  1903. sprintf(accountaor2, "baresip-call-status-%s-%s", account2, domain2);
  1904. if(get_sip_status(accountaor2) != OFFLINE)
  1905. {
  1906. ACCOUNT2_REG = REGISTER_OK;
  1907. }
  1908. }
  1909. GetCmdValue(SPK_CONF, "account_info_3:enable", enac3, sizeof(enac3));
  1910. if(strcmp(enac3, "yes") == 0)
  1911. {
  1912. GetCmdValue(SPK_CONF, "account_info_3:username", account3, sizeof(account3));
  1913. GetCmdValue(SPK_CONF, "account_info_3:domain", domain3, sizeof(domain3));
  1914. if(strlen(domain3) == 0)
  1915. {
  1916. GetCmdValue(SPK_CONF, "account_info_3:server", domain3, sizeof(domain3));
  1917. }
  1918. memset(accountaor3, '\0', sizeof(accountaor3));
  1919. sprintf(accountaor3, "baresip-call-status-%s-%s", account3, domain3);
  1920. if(get_sip_status(accountaor3) != OFFLINE)
  1921. {
  1922. ACCOUNT3_REG = REGISTER_OK;
  1923. }
  1924. }
  1925. GetCmdValue(SPK_CONF, "account_info_p2p:enable", p2penable, sizeof(p2penable));
  1926. if(strcmp(p2penable,"yes") == 0)
  1927. {
  1928. ACCOUNTP2P_REG = REGISTER_OK;
  1929. }
  1930. if(ACCOUNT1_REG == REGISTER_OK || ACCOUNT2_REG == REGISTER_OK || ACCOUNT3_REG == REGISTER_OK || ACCOUNTP2P_REG == REGISTER_OK)
  1931. {
  1932. }
  1933. }
  1934. const int init()
  1935. {
  1936. int ret_thrd1,ret_thrd2,ret_thrd3,ret_thrd4;
  1937. pthread_t thread1, thread2, thread3, thread4;
  1938. redisClient = create_redis_pool_client(unix_socket_path);
  1939. memset(SCHEDULE, 0,sizeof(SCHEDULE));
  1940. sip_reg_status_init();
  1941. //启动线程订阅BARESIP实时状态
  1942. ret_thrd1 = pthread_create(&thread1, NULL, baresip_status, NULL);
  1943. if (ret_thrd1 != 0) {
  1944. printf("create thread1 baresip_status failed\n");
  1945. return FALSE;
  1946. }
  1947. //启动日志记录线程
  1948. ret_thrd2 = pthread_create(&thread2, NULL, write_opnlog, NULL);
  1949. if (ret_thrd2 != 0) {
  1950. printf("create thread2 write_opnlog failed\n");
  1951. return FALSE;
  1952. }
  1953. //启动系统时间显示线程
  1954. ret_thrd3 = pthread_create(&thread3, NULL, handleDateTime, NULL);
  1955. if (ret_thrd3 != 0)
  1956. {
  1957. printf("create thread3 handleDateTime failed\n");
  1958. return FALSE;
  1959. }
  1960. //启动线程订阅音量设置
  1961. ret_thrd4 = pthread_create(&thread4, NULL, get_volume_info, NULL);
  1962. if (ret_thrd4 != 0) {
  1963. printf("create thread4 get_volume_info failed\n");
  1964. return FALSE;
  1965. }
  1966. return TRUE;
  1967. }
  1968. int main(int argc, char *argv[]){
  1969. struct pollfd fdset[2];
  1970. int switch_fd,reset_fd,ret;
  1971. int len, count, i, key1_action, key2_action;
  1972. char buf[8], strtmp[64],action[8],url[256], srceen[8], type[16], input[8];
  1973. time_t lastime, newtime;
  1974. if(argc == 2 && strcmp(argv[1], "debug") == 0)
  1975. DEBUG = TRUE;
  1976. if(!init())
  1977. {
  1978. printf("init failed!\n");
  1979. exit( -1 );
  1980. }
  1981. /* BUTTON */
  1982. switch_fd = open("/sys/class/gpio/gpio111/value", O_RDONLY);
  1983. read(switch_fd, buf, sizeof(buf));
  1984. lseek(switch_fd, 0, SEEK_SET);
  1985. /* RESET */
  1986. reset_fd = open("/sys/class/gpio/gpio73/value", O_RDONLY);
  1987. read(reset_fd, buf, sizeof(buf));
  1988. lseek(reset_fd, 0, SEEK_SET);
  1989. while(1)
  1990. {
  1991. memset(fdset, 0x00, sizeof(struct pollfd) * 2);
  1992. fdset[0].fd = switch_fd;
  1993. fdset[0].events = POLLPRI;
  1994. fdset[1].fd = reset_fd;
  1995. fdset[1].events = POLLPRI;
  1996. ret = poll(fdset, 2, 3000);
  1997. if(ret < 0){
  1998. printf("npoll() faild! \n");
  1999. return -1;
  2000. }
  2001. if(ret == 0)
  2002. continue;
  2003. /*BUTTON*/
  2004. if( fdset[0].revents & POLLPRI )
  2005. {
  2006. count = 0;
  2007. for(i=0;i<100;i++){ // 100ms
  2008. bzero(buf,sizeof(buf));
  2009. read(fdset[0].fd, buf, sizeof(buf));
  2010. lseek(fdset[0].fd, 0, SEEK_SET);
  2011. if(buf[0]-'0' == 0)
  2012. count++;
  2013. usleep(1000);
  2014. }
  2015. // printf("zero counts: %d\n", count);
  2016. if (count >= 70) {
  2017. playBeep();
  2018. if(DEBUG)
  2019. printf("KEY DOWN\n");
  2020. writeLog(BTN, "KEY", "DOWN");
  2021. GetCmdValue(SPK_CONF, "intercom:key_action", action, sizeof(action));
  2022. if(strcmp(action,"call") == 0)
  2023. {
  2024. keyAction(KEY);
  2025. }
  2026. if(strcmp(action,"http") == 0)
  2027. {
  2028. GetCmdValue(SPK_CONF, "intercom:http_url", url, sizeof(url));
  2029. action_url(url);
  2030. writeLog(FUN, "KEY HTTP Request", url);
  2031. }
  2032. else if(strcmp(action,"play") == 0)
  2033. {
  2034. char audiofile[32],repeat_tmp[16];
  2035. GetCmdValue(SPK_CONF, "intercom:audio_file", audiofile, sizeof(audiofile));
  2036. GetCmdValue(SPK_CONF, "intercom:audio_repeat", repeat_tmp, sizeof(repeat_tmp));
  2037. int repeat = atoi(repeat_tmp);
  2038. play_audio(audiofile,repeat);
  2039. sprintf(strtmp, "File: %s; Repeat: %d", audiofile, repeat);
  2040. writeLog( FUN, "KEY Play Auido", strtmp );
  2041. }
  2042. }
  2043. }
  2044. /*RESET*/
  2045. if( fdset[1].revents & POLLPRI )
  2046. {
  2047. count = 0;
  2048. for(i=0;i<100;i++){ // 100ms
  2049. bzero(buf,sizeof(buf));
  2050. read(fdset[1].fd, buf, sizeof(buf));
  2051. lseek(fdset[1].fd, 0, SEEK_SET);
  2052. if(buf[0]-'0' == 0)
  2053. count++;
  2054. usleep(1000);
  2055. }
  2056. if (count >= 70) {
  2057. time(&newtime);
  2058. printf("KEY DOWN\n");
  2059. writeLog(BTN, "RESET", "DOWN");
  2060. } else {
  2061. time(&lastime);
  2062. len = lastime - newtime;
  2063. printf("KEY UP, Time:%d\n", len);
  2064. sprintf(strtmp, "Release <Duration %ds>", len);
  2065. writeLog(BTN, "RESET", strtmp);
  2066. if(len > 10)
  2067. {
  2068. keyAction(KEY2_10);
  2069. }
  2070. else if(len > 3)
  2071. {
  2072. keyAction(KEY2_3);
  2073. }
  2074. }
  2075. }
  2076. }
  2077. }