Module_Wifi.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757
  1. /*
  2. * Module_Wifi.c
  3. *
  4. * Created on: 2020-01-14
  5. * Update on: 2020-06-29
  6. * Author: Jerry Wang, Eason Yang, Folus Wen
  7. * Version: D0.05
  8. */
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/socket.h>
  16. #include <sys/ipc.h>
  17. #include <sys/shm.h>
  18. #include <sys/shm.h>
  19. #include <sys/mman.h>
  20. #include <linux/wireless.h>
  21. #include <arpa/inet.h>
  22. #include <netinet/in.h>
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include <stdio.h> /*標準輸入輸出定義*/
  26. #include <stdlib.h> /*標準函數庫定義*/
  27. #include <unistd.h> /*Unix 標準函數定義*/
  28. #include <fcntl.h> /*檔控制定義*/
  29. #include <termios.h> /*PPSIX 終端控制定義*/
  30. #include <errno.h> /*錯誤號定義*/
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35. #include <ifaddrs.h>
  36. #include <sqlite3.h>
  37. #include "define.h"
  38. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  39. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  40. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  41. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  42. #define PASS 1
  43. #define FAIL -1
  44. #define ON 1
  45. #define OFF 0
  46. #define true 1
  47. #define false 0
  48. #define IP_TYPE_DHCP 0
  49. #define IP_TYPE_STATIC 1
  50. #define AUTH_TYPE_NONE 0
  51. #define AUTH_TYPE_WEP 1
  52. #define AUTH_TYPE_WPAPSK 2
  53. #define AUTH_TYPE_WPA2PSK 3
  54. #define WIFI_MODE_DISABLE 0
  55. #define WIFI_MODE_STA 1
  56. #define WIFI_MODE_AP 2
  57. #define WIFI_MODE_ADHOC 3
  58. #define STA_STATE_INIT 0
  59. #define STA_STATE_DEVICE_DETECT 1
  60. #define STA_STATE_AP_CONNECTED 2
  61. #define STA_STATE_INTERNET_CHECKING 3
  62. #define AP_STATE_INIT 0
  63. #define AP_STATE_DEVICE_DETECT 1
  64. #define AP_STATE_AP_START 2
  65. #define AP_STATE_SERVER_LISTEN 3
  66. #define LISTEN_PORT_UDP 54088
  67. #define LISTEN_PORT_TCP 54089
  68. #define CONNECTION_LIMIT 1
  69. #define MtdBlockSize 0x600000
  70. #define REGISTER 0x01
  71. #define CLEAR_REGISTER 0x02
  72. #define UBLOX
  73. //#define MT7601U
  74. #define DB_FILE "/Storage/ChargeLog/puk_pin_list.db"
  75. enum WIFI_COMMAND
  76. {
  77. WIFI_CMD_LOGIN_SIGNIN = 0x01,
  78. WIFI_CMD_LOGIN_AUTH_PUK = 0x02,
  79. WIFI_CMD_LOGIN_GET_USER_ID = 0x03,
  80. WIFI_CMD_PROTOCOL_VERSION = 0x04,
  81. WIFI_CMD_GET_MODEL_NAME = 0x0d,
  82. WIFI_CMD_GET_SERIAL_NUMBER = 0x0e,
  83. WIFI_CMD_GET_SYSTEM_TIME = 0x0f,
  84. WIFI_CMD_GET_WHITE_CARD = 0x12,
  85. WIFI_CMD_GET_SYS_ALARM_CODE = 0x15,
  86. WIFI_CMD_GET_SYS_ID = 0x16,
  87. WIFI_CMD_GET_RATING_CURRENT = 0x17,
  88. WIFI_CMD_GET_VER_RFS = 0x1e,
  89. WIFI_CMD_GET_VER_PRIMARY_CSU = 0x1f,
  90. WIFI_CMD_GET_VER_LCM = 0x20,
  91. WIFI_CMD_GET_PSU_COUNT = 0x21,
  92. WIFI_CMD_GET_VER_PSU = 0x22,
  93. WIFI_CMD_GET_VER_AUX_POWER = 0x23,
  94. WIFI_CMD_GET_VER_FAN = 0x24,
  95. WIFI_CMD_GET_VER_RELAY = 0x25,
  96. WIFI_CMD_GET_VER_TELECOMM = 0x26,
  97. WIFI_CMD_GET_EVSE_CONFIG = 0x30,
  98. WIFI_CMD_GET_GUN_STS = 0x32,
  99. WIFI_CMD_GET_GUN_USER = 0x33,
  100. WIFI_CMD_GET_SCHEDULE = 0x36,
  101. WIFI_CMD_GET_NETWORK_STS = 0x41,
  102. WIFI_CMD_GET_ETH_DHCP = 0x42,
  103. WIFI_CMD_GET_ETH_MAC = 0x44,
  104. WIFI_CMD_GET_ETH_IP = 0x45,
  105. WIFI_CMD_GET_ETH_SUBMASK = 0x47,
  106. WIFI_CMD_GET_ETH_GATEWAY = 0x49,
  107. WIFI_CMD_GET_WIFI_MODE = 0x54,
  108. WIFI_CMD_GET_WIFI_SSID = 0x56,
  109. WIFI_CMD_GET_WIFI_PASSWD = 0x58,
  110. WIFI_CMD_GET_WIFI_RSSI = 0x5a,
  111. WIFI_CMD_GET_WIFI_DHCP = 0x5b,
  112. WIFI_CMD_GET_WIFI_MAC = 0x5d,
  113. WIFI_CMD_GET_WIFI_IP = 0x5e,
  114. WIFI_CMD_GET_WIFI_NETMASK = 0x60,
  115. WIFI_CMD_GET_WIFI_GATEWAY = 0x62,
  116. WIFI_CMD_GET_TELE_APN = 0x6b,
  117. WIFI_CMD_GET_TELE_RSSI = 0x6d,
  118. WIFI_CMD_GET_TELE_CHAP_ID = 0x6e,
  119. WIFI_CMD_GET_TELE_CHAP_PASSWD = 0x70,
  120. WIFI_CMD_GET_TELE_IMEI = 0x72,
  121. WIFI_CMD_GET_TELE_IMSI = 0x73,
  122. WIFI_CMD_GET_TELE_SIM_STS = 0x74,
  123. WIFI_CMD_GET_TELE_MODE = 0x75,
  124. WIFI_CMD_GET_TELE_IP = 0x76,
  125. WIFI_CMD_SET_SYSTEM_TIME = 0x10,
  126. WIFI_CMD_SET_WHITE_CARD = 0x13,
  127. WIFI_CMD_SET_FACTORY_RESET = 0x14,
  128. WIFI_CMD_SET_EVSE_CONFIG = 0x31,
  129. WIFI_CMD_SET_CHARGING_START = 0x34,
  130. WIFI_CMD_SET_CHARGING_STOP = 0x35,
  131. WIFI_CMD_SET_SCHEDULE = 0x37,
  132. WIFI_CMD_SET_ETH_CONFIG = 0x43,
  133. WIFI_CMD_SET_ETH_IP = 0x46,
  134. WIFI_CMD_SET_ETH_SUBMASK = 0x48,
  135. WIFI_CMD_SET_ETH_GATEWAY = 0x4a,
  136. WIFI_CMD_SET_WIFI_MODE = 0x55,
  137. WIFI_CMD_SET_WIFI_SSID = 0x57,
  138. WIFI_CMD_SET_WIFI_PASSWD = 0x59,
  139. WIFI_CMD_SET_WIFI_DHCP = 0x5c,
  140. WIFI_CMD_SET_WIFI_IP = 0x5f,
  141. WIFI_CMD_SET_WIFI_NETMASK = 0x61,
  142. WIFI_CMD_SET_WIFI_GATEWAY = 0x63,
  143. WIFI_CMD_SET_TELE_APN = 0x6c,
  144. WIFI_CMD_SET_TELE_CHAP_ID = 0x6f,
  145. WIFI_CMD_SET_TELE_CHAP_PASSWD = 0x71,
  146. WIFI_CMD_SET_TELE_IP = 0x77,
  147. WIFI_CMD_SET_CONFIRM = 0x81,
  148. WIFI_CMD_SET_DISCARD = 0x82,
  149. WIFI_CMD_UNKNOW = 0xfe
  150. };
  151. enum LOGIN_ROLE
  152. {
  153. ROLE_UNKNOWN = 0x00,
  154. ROLE_MANAGER = 0x01,
  155. ROLE_USER = 0x02
  156. };
  157. struct SYS_FLAG
  158. {
  159. uint8_t isPkill;
  160. }Sys_Flag;
  161. struct interface_info
  162. {
  163. int ipType;
  164. int authType;
  165. int rssi;
  166. int cnt_InternetFail;
  167. char *currentInterface;
  168. char currentSSID[256];
  169. char currentPasswd[256];
  170. char currentMAC[24];
  171. char currentIPAddr[24];
  172. char currentNetmask[24];
  173. char currentGateway[24];
  174. char staticIPAddr[24];
  175. }Wifi;
  176. struct Display_Request
  177. {
  178. unsigned char isShowed_IF_info:1;
  179. unsigned char isShowed_IP_info:1;
  180. }dispReq;
  181. struct auth_info
  182. {
  183. int auth_type;
  184. unsigned char ssid[256];
  185. unsigned char passwd[256];
  186. }Wifi_A;
  187. struct WIFI_LOGIN_INFO
  188. {
  189. uint8_t puk[8];
  190. uint8_t loginRole;
  191. uint8_t loginId[32];
  192. }wifi_login_info;
  193. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  194. struct StatusCodeData *ShmStatusCodeData;
  195. struct OCPP16Data *ShmOCPP16Data;
  196. struct PsuData *ShmPsuData;
  197. struct SysConfigData SysConfigTemporarily;
  198. int isFindInterface();
  199. int getInterfaceInfo();
  200. int isReachableInternet();
  201. int getLinkQuality();
  202. char* getTimeString(void);
  203. int setWPAconf();
  204. int restartWPA();
  205. void refreshUSB();
  206. void getParameters();
  207. int Wifi_module_sts = STA_STATE_INIT;
  208. int isSetWPA_OK = false;
  209. int cnt_pingDNS_Fail;
  210. int cnt_getAP_Fail;
  211. int cnt_getIP_Fail;
  212. sqlite3 *localDb;
  213. void trim(char *s);
  214. int mystrcmp(char *p1,char *p2);
  215. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  216. void split(char **arr, char *str, const char *del);
  217. int isValidCheckSum(uint8_t *message);
  218. char *Support_InterfaceSTA[2] = {"mlan0", "wlan0"};
  219. char *Support_InterfaceAP[1] = {"uap0"};
  220. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  221. int protocol_Version [] = {0,6,0};
  222. int StoreLogMsg(const char *fmt, ...)
  223. {
  224. char Buf[4096+256];
  225. char buffer[4096];
  226. time_t CurrentTime;
  227. struct tm *tm;
  228. va_list args;
  229. va_start(args, fmt);
  230. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  231. va_end(args);
  232. memset(Buf,0,sizeof(Buf));
  233. CurrentTime = time(NULL);
  234. tm=localtime(&CurrentTime);
  235. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]Wifi_SystemLog",
  236. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  237. buffer,
  238. tm->tm_year+1900,tm->tm_mon+1);
  239. #ifdef SystemLogMessage
  240. system(Buf);
  241. #endif
  242. #ifdef ConsloePrintLog
  243. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  244. #endif
  245. return rc;
  246. }
  247. int DiffTimeb(struct timeb ST, struct timeb ET)
  248. {
  249. //return milli-second
  250. unsigned int StartTime,StopTime;
  251. StartTime=(unsigned int)ST.time;
  252. StopTime=(unsigned int)ET.time;
  253. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  254. }
  255. //==========================================
  256. // Init all share memory
  257. //==========================================
  258. int InitShareMemory()
  259. {
  260. int result = PASS;
  261. int MeterSMId;
  262. //creat ShmSysConfigAndInfo
  263. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  264. {
  265. #ifdef SystemLogMessage
  266. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  267. #endif
  268. result = FAIL;
  269. }
  270. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  271. {
  272. #ifdef SystemLogMessage
  273. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  274. #endif
  275. result = FAIL;
  276. }
  277. else
  278. {}
  279. //creat ShmStatusCodeData
  280. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  281. {
  282. #ifdef SystemLogMessage
  283. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  284. #endif
  285. result = FAIL;
  286. }
  287. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  288. {
  289. #ifdef SystemLogMessage
  290. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  291. #endif
  292. result = FAIL;
  293. }
  294. else
  295. {}
  296. //creat ShmOCPP16Data
  297. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  298. {
  299. #ifdef SystemLogMessage
  300. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  301. #endif
  302. result = FAIL;
  303. }
  304. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  305. {
  306. #ifdef SystemLogMessage
  307. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  308. #endif
  309. result = FAIL;
  310. }
  311. else
  312. {}
  313. //creat ShmPsuData
  314. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  315. {
  316. #ifdef SystemLogMessage
  317. DEBUG_ERROR("shmget ShmPsuData NG \n");
  318. #endif
  319. result = FAIL;
  320. }
  321. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  322. {
  323. #ifdef SystemLogMessage
  324. DEBUG_ERROR("shmat ShmPsuData NG \n");
  325. #endif
  326. result = FAIL;
  327. }
  328. memset(ShmPsuData,0,sizeof(struct PsuData));
  329. return result;
  330. }
  331. //==========================================
  332. // Get parameters from shared memory
  333. //==========================================
  334. void getParameters(void)
  335. {
  336. DEBUG_INFO("============= Configuration ===============\r\n");
  337. strcpy((char*)Wifi_A.ssid, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  338. DEBUG_INFO("Wifi SSID: %s\n", Wifi_A.ssid);
  339. strcpy((char*)Wifi_A.passwd, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  340. DEBUG_INFO("Wifi Password: %s\n", Wifi_A.passwd);
  341. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress != NULL)
  342. strcpy((char*)Wifi.staticIPAddr, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  343. DEBUG_INFO("Wifi Static IP: %s\n", Wifi.staticIPAddr);
  344. Wifi.ipType = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient;
  345. DEBUG_INFO("DHCP client flag: %d\n", Wifi.ipType);
  346. DEBUG_INFO("===========================================\r\n");
  347. }
  348. //==========================================
  349. // Refresh USB interface
  350. //==========================================
  351. void refreshUSB(void)
  352. {
  353. FILE *fp;
  354. char cmd[256];
  355. // Get IP address & net mask
  356. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
  357. fp = popen(cmd, "r");
  358. sleep(2);
  359. pclose(fp);
  360. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
  361. fp = popen(cmd, "r");
  362. sleep(5);
  363. pclose(fp);
  364. }
  365. //==========================================
  366. // Check wifi interface status
  367. //==========================================
  368. int isFindInterface()
  369. {
  370. int result = FAIL;
  371. struct ifaddrs *ifaddr, *ifa;
  372. //refreshUSB();
  373. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == WIFI_MODE_AP)
  374. {
  375. if (getifaddrs(&ifaddr) != FAIL)
  376. {
  377. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  378. {
  379. if (ifa->ifa_addr == NULL ||
  380. ifa->ifa_addr->sa_family != AF_PACKET) continue;
  381. for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceAP);idx++)
  382. {
  383. if(mystrcmp(ifa->ifa_name, Support_InterfaceAP[idx]) == PASS)
  384. {
  385. Wifi.currentInterface = ifa->ifa_name;
  386. result = PASS;
  387. }
  388. }
  389. }
  390. freeifaddrs(ifaddr);
  391. freeifaddrs(ifa);
  392. }
  393. }
  394. else
  395. {
  396. if (getifaddrs(&ifaddr) != FAIL)
  397. {
  398. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  399. {
  400. if (ifa->ifa_addr == NULL ||
  401. ifa->ifa_addr->sa_family != AF_PACKET) continue;
  402. for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceSTA);idx++)
  403. {
  404. if(mystrcmp(ifa->ifa_name, Support_InterfaceSTA[idx]) == PASS)
  405. {
  406. Wifi.currentInterface = ifa->ifa_name;
  407. result = PASS;
  408. }
  409. }
  410. }
  411. freeifaddrs(ifaddr);
  412. freeifaddrs(ifa);
  413. }
  414. }
  415. return result;
  416. }
  417. //==========================================
  418. // Get wifi interface info
  419. //==========================================
  420. int getInterfaceInfo()
  421. {
  422. int result = PASS;
  423. FILE *fp;
  424. char cmd[256];
  425. char buf[512];
  426. char tmp[512];
  427. // Get IP address & net mask
  428. strcpy(cmd, "ifconfig ");
  429. strcat(cmd, Wifi.currentInterface);
  430. fp = popen(cmd, "r");
  431. if(fp == NULL)
  432. result = FAIL;
  433. else
  434. {
  435. while(fgets(buf, sizeof(buf), fp) != NULL)
  436. {
  437. if(strstr(buf, "HWaddr") > 0)
  438. {
  439. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  440. strcpy(Wifi.currentMAC, tmp);
  441. }
  442. if(strstr(buf, "inet addr:") > 0)
  443. {
  444. sscanf(buf, "%*s%s", tmp);
  445. substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  446. sscanf(buf, "%*s%*s%*s%s", tmp);
  447. substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  448. }
  449. }
  450. }
  451. pclose(fp);
  452. // Get gateway
  453. fp = popen("ip route", "r");
  454. if(fp == NULL)
  455. result = FAIL;
  456. else
  457. {
  458. while(fgets(buf, sizeof(buf), fp) != NULL)
  459. {
  460. if(strncmp(buf, "default", strlen("default")) == 0)
  461. break;
  462. }
  463. sscanf(buf, "%*s%*s%s", tmp);
  464. substr(Wifi.currentGateway, tmp, 0, strlen(tmp));
  465. }
  466. pclose(fp);
  467. if(!dispReq.isShowed_IF_info)
  468. {
  469. DEBUG_INFO("MAC: %s\n", Wifi.currentMAC);
  470. dispReq.isShowed_IF_info = true;
  471. }
  472. if(!dispReq.isShowed_IP_info)
  473. {
  474. DEBUG_INFO("IP address: %s\n", Wifi.currentIPAddr);
  475. DEBUG_INFO("Net mask: %s\n", Wifi.currentNetmask);
  476. DEBUG_INFO("Default gateway: %s\n", Wifi.currentGateway);
  477. dispReq.isShowed_IP_info = true;
  478. }
  479. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, (const char*)Wifi.currentIPAddr);
  480. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, (const char*)Wifi.currentNetmask);
  481. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, (const char*)Wifi.currentGateway);
  482. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, (const char*)Wifi.currentMAC);
  483. return result;
  484. }
  485. //==========================================
  486. // Check if module connect to wifi AP
  487. //==========================================
  488. int isConnectAP()
  489. {
  490. int result = PASS;
  491. FILE *fp;
  492. char cmd[256];
  493. char buf[512];
  494. // Get IP address & net mask
  495. strcpy(cmd, "iwconfig ");
  496. strcat(cmd, Wifi.currentInterface);
  497. fp = popen(cmd, "r");
  498. if(fp == NULL)
  499. result = FAIL;
  500. else
  501. {
  502. while(fgets(buf, sizeof(buf), fp) != NULL)
  503. {
  504. if(strstr(buf, "Not-Associated") > 0)
  505. {
  506. result = FAIL;
  507. DEBUG_INFO("AP check: Not-Associated.\n");
  508. }
  509. }
  510. }
  511. pclose(fp);
  512. return result;
  513. }
  514. //==========================================
  515. // Check internet access status
  516. //==========================================
  517. int isReachableInternet()
  518. {
  519. int result = FAIL;
  520. FILE *fp;
  521. char cmd[256];
  522. char buf[512];
  523. //char tmp[512];
  524. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  525. {
  526. sprintf(cmd, "ping -c 1 -w 3 -I %s %s", Wifi.currentInterface, valid_Internet[idx]);
  527. fp = popen(cmd, "r");
  528. if(fp != NULL)
  529. {
  530. while(fgets(buf, sizeof(buf), fp) != NULL)
  531. {
  532. if(strstr(buf, "transmitted") > 0)
  533. {
  534. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  535. if(strstr(buf,"100%") != NULL)
  536. {
  537. }
  538. else
  539. {
  540. result = PASS;
  541. }
  542. //DEBUG_INFO("%s",buf);
  543. //DEBUG_INFO("%s\n",tmp);
  544. }
  545. }
  546. }
  547. pclose(fp);
  548. }
  549. return result;
  550. }
  551. //==========================================
  552. // Check module soft AP is start up
  553. //==========================================
  554. int isStartUpAP()
  555. {
  556. int result = FAIL;
  557. FILE *fp;
  558. char cmd[256];
  559. char buf[512];
  560. // Get IP address & net mask
  561. strcpy(cmd, "/root/uaputl sys_cfg_bss_status");
  562. fp = popen(cmd, "r");
  563. if(fp != NULL)
  564. {
  565. while(fgets(buf, sizeof(buf), fp) != NULL)
  566. {
  567. if(strstr(buf, "BSS status = started") > 0)
  568. {
  569. result = PASS;
  570. DEBUG_INFO("AP already start up.\n");
  571. }
  572. }
  573. }
  574. pclose(fp);
  575. return result;
  576. }
  577. //==========================================
  578. // Get wifi connection quality
  579. //==========================================
  580. int getLinkQuality()
  581. {
  582. int result = FAIL;
  583. FILE *fp;
  584. char cmd[256];
  585. char buf[512];
  586. char tmp[512];
  587. strcpy(cmd, "iwconfig ");
  588. strcat(cmd, Wifi.currentInterface);
  589. fp = popen(cmd, "r");
  590. if(fp == NULL)
  591. result = FAIL;
  592. else
  593. {
  594. while(fgets(buf, sizeof(buf), fp) != NULL)
  595. {
  596. if(strstr(buf, "Signal level") > 0)
  597. {
  598. sscanf(buf, "%*s%*s%*s%s", tmp);
  599. substr(tmp, tmp, strspn(tmp, "Signal level="), strlen(buf)-strspn(tmp, "Signal level="));
  600. Wifi.rssi = atoi(tmp);
  601. result = PASS;
  602. }
  603. }
  604. }
  605. pclose(fp);
  606. return result;
  607. }
  608. //==========================================
  609. // WPA parameter set
  610. //==========================================
  611. int setWPAconf()
  612. {
  613. int result = FAIL;
  614. char cmdBuf[512];
  615. FILE *pFile;
  616. char buffer[500]="ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\n";
  617. // Stop dhcp client or server
  618. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  619. system(cmdBuf);
  620. sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
  621. system(cmdBuf);
  622. // Clear wifi configuration file
  623. system("cat /dev/null > /etc/wpa.conf");
  624. system("cat /dev/null > /etc/hostapd.conf");
  625. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  626. {
  627. case WIFI_MODE_STA:
  628. // Fill out configuration value depend on share memory setting
  629. if(strlen((const char*)Wifi_A.passwd)>0)
  630. {
  631. if(strlen((const char*)Wifi_A.passwd)>=8)
  632. {
  633. strcat(buffer, "network={\n ssid=\"");
  634. strcat(buffer, (char*)Wifi_A.ssid);
  635. strcat(buffer, "\"\n key_mgmt=WPA-EAP WPA-PSK \n");
  636. strcat(buffer, " psk=\"");
  637. strcat(buffer, (char*)Wifi_A.passwd);
  638. strcat(buffer, "\"\n}\n\n");
  639. }
  640. if(strlen((const char*)Wifi_A.passwd)==5 || strlen((const char*)Wifi_A.passwd)==13)
  641. {
  642. strcat(buffer, "network={\n ssid=\"");
  643. strcat(buffer, (char*)Wifi_A.ssid);
  644. strcat(buffer, "\"\n key_mgmt=NONE\n");
  645. strcat(buffer, " wep_key0=\"");
  646. strcat(buffer, (char*)Wifi_A.passwd);
  647. strcat(buffer, "\"\n auth_alg=OPEN SHARED\n");
  648. strcat(buffer, "\n}\n\n");
  649. }
  650. strcat(buffer, "network={\n ssid=\"");
  651. strcat(buffer, (char*)Wifi_A.ssid);
  652. strcat(buffer, "\"\n key_mgmt=NONE");
  653. strcat(buffer, "\n}");
  654. }
  655. else
  656. {
  657. strcat(buffer, "network={\n ssid=\"");
  658. strcat(buffer, (char*)Wifi_A.ssid);
  659. strcat(buffer, "\"\n key_mgmt=NONE");
  660. strcat(buffer, "\n}");
  661. }
  662. // Configuration value write to /etc/wpa.conf
  663. pFile = fopen("/etc/wpa.conf","w");
  664. fwrite(buffer,strlen(buffer), 1, pFile);
  665. fclose(pFile);
  666. // Restart wpa_supplicant
  667. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  668. system(cmdBuf);
  669. sprintf(cmdBuf, "/root/wpa_supplicant -i %s -c /etc/wpa.conf -B", Wifi.currentInterface);
  670. system(cmdBuf);
  671. // Stop wifi AP
  672. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  673. system(cmdBuf);
  674. sleep(5);
  675. // Wifi IP set by DHCP client or static
  676. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
  677. {
  678. sprintf(cmdBuf, "/sbin/udhcpc -i %s -x hostname:CSU3_%s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface, ShmSysConfigAndInfo->SysConfig.SystemId);
  679. system(cmdBuf);
  680. }
  681. else
  682. {
  683. memset(cmdBuf, 0, ARRAY_SIZE(cmdBuf));
  684. sprintf(cmdBuf,"/sbin/ifconfig %s %s netmask %s",
  685. Wifi.currentInterface,
  686. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress,
  687. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  688. system(cmdBuf);
  689. memset(cmdBuf, 0, ARRAY_SIZE(cmdBuf));
  690. sprintf(cmdBuf,"route add default gw %s %s ",
  691. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress,
  692. Wifi.currentInterface);
  693. system(cmdBuf);
  694. DEBUG_INFO("Setting %s static IP to %s submask to %s gateway to %s...\n",
  695. Wifi.currentInterface,
  696. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress,
  697. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress,
  698. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  699. }
  700. break;
  701. case WIFI_MODE_AP:
  702. // Stop dhcp client or server
  703. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  704. system(cmdBuf);
  705. sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
  706. system(cmdBuf);
  707. // Restart wpa_supplicant
  708. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  709. system(cmdBuf);
  710. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  711. system(cmdBuf);
  712. sprintf(cmdBuf, "echo interface=uap0 >> /etc/hostapd.conf");
  713. system(cmdBuf);
  714. sprintf(cmdBuf, "echo driver=nl80211 >> /etc/hostapd.conf");
  715. system(cmdBuf);
  716. sprintf(cmdBuf, "echo ssid=%s%s >> /etc/hostapd.conf", ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber);
  717. system(cmdBuf);
  718. sprintf(cmdBuf, "echo channel=6 >> /etc/hostapd.conf");
  719. system(cmdBuf);
  720. sprintf(cmdBuf, "echo wpa=2 >> /etc/hostapd.conf");
  721. system(cmdBuf);
  722. sprintf(cmdBuf, "echo wpa_passphrase=%s%s >> /etc/hostapd.conf", ShmSysConfigAndInfo->SysConfig.SerialNumber, ShmSysConfigAndInfo->SysConfig.ModelName);
  723. system(cmdBuf);
  724. sprintf(cmdBuf, "echo wpa_key_mgmt=WPA-PSK >> /etc/hostapd.conf");
  725. system(cmdBuf);
  726. sprintf(cmdBuf, "hostapd /etc/hostapd.conf -B");
  727. system(cmdBuf);
  728. sleep(5);
  729. // Set wifi AP ip address
  730. sprintf(cmdBuf, "ifconfig %s 192.168.10.10", Wifi.currentInterface);
  731. system(cmdBuf);
  732. DEBUG_INFO("Setting %s static IP to 192.168.10.10...\n", Wifi.currentInterface);
  733. // Start DHCP server
  734. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpServer==0)
  735. {
  736. sprintf(cmdBuf, "/usr/sbin/udhcpd /etc/udhcpd.conf > /dev/null &");
  737. system(cmdBuf);
  738. }
  739. break;
  740. case WIFI_MODE_ADHOC:
  741. break;
  742. case WIFI_MODE_DISABLE:
  743. default:
  744. break;
  745. }
  746. result = PASS;
  747. return result;
  748. }
  749. //==========================================
  750. // UDP socket server routine
  751. //==========================================
  752. int split2int(char *dest, char *src, const char *flag, int base)
  753. {
  754. uint8_t result = 0;
  755. char *s = strtok(src, flag);
  756. while(s != NULL)
  757. {
  758. *dest++ = (base==10?atoi(s):strtol(s, NULL, 16));
  759. s = strtok(NULL, flag);
  760. result++;
  761. }
  762. return result;
  763. }
  764. uint8_t parseIP(uint8_t *ip)
  765. {
  766. uint8_t result = FAIL;
  767. const int8_t flag_dot[1] = {'.'};
  768. int8_t buf[16];
  769. memcpy(buf, ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, ARRAY_SIZE(buf));
  770. DEBUG_INFO("GET IP WifiIpAddress : %s...\r\n",ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  771. if(split2int((char*)ip , (char*)buf, (char*)flag_dot, 10)==4)
  772. result = PASS;
  773. return result;
  774. }
  775. uint8_t parseMAC(uint8_t *mac)
  776. {
  777. uint8_t result = FAIL;
  778. const int8_t flag_colon[1] = {':'};
  779. int8_t buf[18];
  780. memcpy(buf, ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, ARRAY_SIZE(buf));
  781. if(split2int((char*)mac , (char*)buf, (char*)flag_colon, 16)==6)
  782. result = PASS;
  783. return result;
  784. }
  785. int udpSocketServerStart(void)
  786. {
  787. int sockFd;
  788. struct sockaddr_in servaddr;
  789. struct sockaddr_in peeraddr;
  790. socklen_t peerlen = sizeof(peeraddr);
  791. uint8_t inputBuffer[2048] = {};
  792. uint8_t outBuffer[2048] = {};
  793. int16_t read_size;
  794. int16_t tx_size;
  795. uint8_t broadcastCmd[6] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x02};
  796. uint8_t validResult;
  797. uint8_t chksum;
  798. uint8_t ip[4];
  799. uint8_t mac[6];
  800. memset(&servaddr, 0, sizeof(servaddr));
  801. servaddr.sin_family = AF_INET;
  802. servaddr.sin_port = htons(LISTEN_PORT_UDP);
  803. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  804. if ((sockFd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  805. return FAIL;
  806. if (bind(sockFd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
  807. return FAIL;
  808. for(;;)
  809. {
  810. if((read_size = recvfrom(sockFd, inputBuffer, sizeof(inputBuffer), 0, (struct sockaddr *)&peeraddr, &peerlen)) > 0)
  811. {
  812. DEBUG_INFO("Revieve from: %s:%d\r\n", inet_ntoa(peeraddr.sin_addr), htons(peeraddr.sin_port));
  813. DEBUG_INFO("read_size: %d\r\n",read_size);
  814. if(read_size>=6)
  815. {
  816. validResult = PASS;
  817. for(uint8_t idx=0;idx<6;idx++)
  818. {
  819. if(inputBuffer[idx] != broadcastCmd[idx])
  820. {
  821. validResult = FAIL;
  822. break;
  823. }
  824. }
  825. if(validResult)
  826. {
  827. memset(outBuffer, 0x00, ARRAY_SIZE(outBuffer));
  828. tx_size = 41;
  829. outBuffer[0] = 0xff;
  830. outBuffer[1] = 0xff;
  831. outBuffer[2] = (0x25 << 0x08) & 0xff;
  832. outBuffer[3] = 0x25 & 0xff;
  833. outBuffer[4] = 0x00;
  834. if(parseIP(ip)==PASS)
  835. {
  836. outBuffer[5] = ip[0];
  837. outBuffer[6] = ip[1];
  838. outBuffer[7] = ip[2];
  839. outBuffer[8] = ip[3];
  840. }
  841. else
  842. {
  843. outBuffer[5] = 0x00;
  844. outBuffer[6] = 0x00;
  845. outBuffer[7] = 0x00;
  846. outBuffer[8] = 0x00;
  847. }
  848. for(uint8_t count = 5; count < 9 ;count++)
  849. DEBUG_INFO("outBuffer[%d] : %X...\r\n",count, outBuffer[count]);
  850. if(parseMAC(mac)==PASS)
  851. {
  852. outBuffer[9] = mac[0];
  853. outBuffer[10] = mac[1];
  854. outBuffer[11] = mac[2];
  855. outBuffer[12] = mac[3];
  856. outBuffer[13] = mac[4];
  857. outBuffer[14] = mac[5];
  858. }
  859. else
  860. {
  861. outBuffer[9] = 0x00;
  862. outBuffer[10] = 0x00;
  863. outBuffer[11] = 0x00;
  864. outBuffer[12] = 0x00;
  865. outBuffer[13] = 0x00;
  866. outBuffer[14] = 0x00;
  867. }
  868. for(uint8_t count = 9; count < 15 ;count++)
  869. DEBUG_INFO("outBuffer[%d] : %X...\r\n",count, outBuffer[count]);
  870. memcpy(&outBuffer[15], ShmSysConfigAndInfo->SysConfig.ModelName, 0x0e);
  871. memcpy(&outBuffer[29], ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x0b);
  872. chksum = 0x00;
  873. for(uint8_t idx=0;idx<36;idx++)
  874. {
  875. chksum ^= outBuffer[idx];
  876. }
  877. outBuffer[40] = chksum;
  878. sendto(sockFd, outBuffer, tx_size, 0, (struct sockaddr *)&peeraddr, peerlen);
  879. }
  880. }
  881. }
  882. }
  883. return FAIL;
  884. }
  885. //===============================================
  886. // SQLite3 related routine
  887. //===============================================
  888. int DB_Open(sqlite3 *db)
  889. {
  890. int result = PASS;
  891. char* errMsg = NULL;
  892. char* createSql="CREATE TABLE IF NOT EXISTS list("
  893. "idx integer primary key AUTOINCREMENT, "
  894. "list_type text, "
  895. "code text, "
  896. "user_id text, "
  897. "isReg text"
  898. ");";
  899. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  900. if(sqlite3_open(DB_FILE, &db))
  901. {
  902. result = FAIL;
  903. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  904. sqlite3_close(db);
  905. }
  906. else
  907. {
  908. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  909. if (sqlite3_exec(db, createSql, 0, 0, &errMsg) != SQLITE_OK)
  910. {
  911. result = FAIL;
  912. DEBUG_INFO( "Create puk/pin list table error message: %s\n", errMsg);
  913. }
  914. else
  915. {
  916. DEBUG_INFO( "Opened puk/pin list table successfully\n");
  917. }
  918. sqlite3_close(db);
  919. }
  920. return result;
  921. }
  922. uint8_t isPukReg(sqlite3 *db)
  923. {
  924. uint8_t result = false;
  925. char* errMsg = NULL;
  926. char sqlStr[1024];
  927. char **rs;
  928. int rows, cols;
  929. sprintf(sqlStr, "select * from list where list_type='puk';");
  930. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  931. if(sqlite3_open(DB_FILE, &db))
  932. {
  933. result = FAIL;
  934. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  935. sqlite3_close(db);
  936. }
  937. else
  938. {
  939. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  940. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  941. if(rows>0)
  942. result = true;
  943. sqlite3_free_table(rs);
  944. sqlite3_close(db);
  945. }
  946. return result;
  947. }
  948. uint8_t PukReg(sqlite3 *db)
  949. {
  950. uint8_t result = false;
  951. char* errMsg = NULL;
  952. char sqlStr[1024];
  953. sprintf(sqlStr, "insert into list(list_type, code, user_id, isReg) "
  954. "values('puk', '%s', '%s', '1');",
  955. wifi_login_info.puk,
  956. wifi_login_info.loginId);
  957. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  958. if(sqlite3_open(DB_FILE, &db))
  959. {
  960. result = FAIL;
  961. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  962. sqlite3_close(db);
  963. }
  964. else
  965. {
  966. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  967. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  968. {
  969. result = FAIL;
  970. DEBUG_INFO( "Insert puk/pin list error message: %s\n", errMsg);
  971. }
  972. else
  973. {
  974. DEBUG_INFO( "Insert puk/pin list successfully\n");
  975. }
  976. sqlite3_close(db);
  977. }
  978. return result;
  979. }
  980. uint8_t isPinGen(sqlite3 *db)
  981. {
  982. uint8_t result = false;
  983. char* errMsg = NULL;
  984. char sqlStr[1024];
  985. char **rs;
  986. int rows, cols;
  987. sprintf(sqlStr, "select * from list where list_type='pin';");
  988. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  989. if(sqlite3_open(DB_FILE, &db))
  990. {
  991. result = FAIL;
  992. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  993. sqlite3_close(db);
  994. }
  995. else
  996. {
  997. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  998. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  999. if(rows>0)
  1000. result = true;
  1001. sqlite3_free_table(rs);
  1002. sqlite3_close(db);
  1003. }
  1004. return result;
  1005. }
  1006. uint8_t PinGen(sqlite3 *db)
  1007. {
  1008. uint8_t result = false;
  1009. char* errMsg = NULL;
  1010. char sqlStr[1024];
  1011. srand(time(NULL));
  1012. if(sqlite3_open(DB_FILE, &db))
  1013. {
  1014. result = FAIL;
  1015. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  1016. sqlite3_close(db);
  1017. }
  1018. else
  1019. {
  1020. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  1021. sprintf(sqlStr, "delete from list where list_type='pin';");
  1022. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1023. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1024. {
  1025. result = FAIL;
  1026. DEBUG_INFO( "Delete puk/pin list error message: %s\n", errMsg);
  1027. }
  1028. else
  1029. {
  1030. for(int idxList=0;idxList<20;idxList++)
  1031. {
  1032. sprintf(sqlStr, "insert into list(list_type, code, user_id, isReg) "
  1033. "values('pin', '%06X', '', '0');", (rand() % 0xffffff));
  1034. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1035. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1036. {
  1037. result = FAIL;
  1038. DEBUG_INFO( "Insert puk/pin list error message: %s\n", errMsg);
  1039. }
  1040. else
  1041. {
  1042. DEBUG_INFO( "Insert puk/pin list successfully\n");
  1043. }
  1044. }
  1045. }
  1046. sqlite3_close(db);
  1047. }
  1048. return result;
  1049. }
  1050. uint8_t isValidPin(sqlite3 *db, uint8_t *pinCode)
  1051. {
  1052. uint8_t result = false;
  1053. char* errMsg = NULL;
  1054. char sqlStr[1024];
  1055. char **rs;
  1056. int rows, cols;
  1057. sprintf(sqlStr, "select * from list where list_type='pin' and code='%s';", pinCode);
  1058. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1059. if(sqlite3_open(DB_FILE, &db))
  1060. {
  1061. result = FAIL;
  1062. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  1063. sqlite3_close(db);
  1064. }
  1065. else
  1066. {
  1067. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  1068. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1069. if(rows>0)
  1070. result = true;
  1071. sqlite3_free_table(rs);
  1072. sqlite3_close(db);
  1073. }
  1074. return result;
  1075. }
  1076. uint8_t loginValid(sqlite3 *db)
  1077. {
  1078. uint8_t result = ROLE_UNKNOWN;
  1079. char* errMsg = NULL;
  1080. char sqlStr[1024];
  1081. char **rs;
  1082. int rows, cols;
  1083. sprintf(sqlStr, "select * from list where user_id='%s';", wifi_login_info.loginId);
  1084. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1085. DEBUG_INFO("Login id: %s\r\n", wifi_login_info.loginId);
  1086. if(sqlite3_open(DB_FILE, &db))
  1087. {
  1088. result = FAIL;
  1089. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  1090. sqlite3_close(db);
  1091. }
  1092. else
  1093. {
  1094. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  1095. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1096. for(int idxRow=0;idxRow<rows;idxRow++)
  1097. {
  1098. if(strcmp(rs[(idxRow*cols)+1], "puk") == 0)
  1099. {
  1100. result = ROLE_MANAGER;
  1101. DEBUG_INFO("Login role: Manager\r\n");
  1102. }
  1103. else if(strcmp(rs[(idxRow*cols)+1], "pin") == 0)
  1104. {
  1105. result = ROLE_USER;
  1106. DEBUG_INFO("Login role: User\r\n");
  1107. }
  1108. }
  1109. sqlite3_free_table(rs);
  1110. sqlite3_close(db);
  1111. }
  1112. return result;
  1113. }
  1114. //==========================================
  1115. // TCP socket server routine
  1116. //==========================================
  1117. uint32_t stm32crc(uint32_t *ptr, uint32_t len)
  1118. {
  1119. uint32_t xbit, data;
  1120. uint32_t crc32 = 0xFFFFFFFF;
  1121. const uint32_t polynomial = 0x04c11db7;
  1122. for(uint8_t i = 0; i < len; i ++)
  1123. {
  1124. xbit = 1 << 31;
  1125. data = ptr[i];
  1126. for (uint32_t bits = 0; bits < 32; bits++)
  1127. {
  1128. if (crc32 & 0x80000000)
  1129. {
  1130. crc32 <<= 1;
  1131. crc32 ^= polynomial;
  1132. }
  1133. else
  1134. crc32 <<= 1;
  1135. if (data & xbit)
  1136. crc32 ^= polynomial;
  1137. xbit >>= 1;
  1138. }
  1139. }
  1140. return crc32;
  1141. }
  1142. int isValidCheckSum(uint8_t *message)
  1143. {
  1144. uint8_t chksum=0;
  1145. for(int idx=0;idx<(((message[2]<<8) | message[3])>1024?1024:(((message[2]<<8) | message[3])-1));idx++)
  1146. {
  1147. chksum ^= message[4+idx];
  1148. }
  1149. return ((chksum == message[3+(((message[2]<<8) | message[3])>1024?1024:((message[2]<<8) | message[3]))]) ? PASS : FAIL);
  1150. }
  1151. int chksumCal(uint8_t *message)
  1152. {
  1153. uint8_t chksum=0;
  1154. for(int idx = 0;idx<(((message[2]<<8) | message[3])>1024?1024:(((message[2]<<8) | message[3])-1));idx++)
  1155. {
  1156. chksum ^= message[4+idx];
  1157. }
  1158. return chksum & 0xff;
  1159. }
  1160. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1161. {
  1162. int result = PASS;
  1163. int fd,wrd;
  1164. unsigned int i,Chk;
  1165. unsigned char *ptr, *BufTmp;
  1166. Chk=0;
  1167. ptr=(unsigned char *)UsrData;
  1168. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1169. {
  1170. memset(BufTmp,0,MtdBlockSize);
  1171. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1172. for(i=0;i<MtdBlockSize-4;i++)
  1173. Chk+=*(BufTmp+i);
  1174. memcpy( BufTmp+MtdBlockSize-4,&Chk,4);
  1175. fd = open("/dev/mtdblock10", O_RDWR);
  1176. if (fd>0)
  1177. {
  1178. wrd=write(fd, BufTmp, MtdBlockSize);
  1179. close(fd);
  1180. if(wrd>=MtdBlockSize)
  1181. {
  1182. fd = open("/dev/mtdblock11", O_RDWR);
  1183. if (fd>0)
  1184. {
  1185. wrd=write(fd, BufTmp, MtdBlockSize);
  1186. close(fd);
  1187. if(wrd<MtdBlockSize)
  1188. {
  1189. DEBUG_ERROR("write /dev/mtdblock11(backup) NG\r\n");
  1190. result = FAIL;
  1191. }
  1192. }
  1193. else
  1194. {
  1195. DEBUG_ERROR("open /dev/mtdblock11(backup) NG\r\n");
  1196. result = FAIL;
  1197. }
  1198. }
  1199. else
  1200. {
  1201. DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
  1202. result = FAIL;
  1203. }
  1204. }
  1205. else
  1206. {
  1207. DEBUG_ERROR("open /dev/mtdblock10 NG\r\n");
  1208. result = FAIL;
  1209. }
  1210. }
  1211. else
  1212. {
  1213. DEBUG_ERROR("alloc BlockSize NG\r\n");
  1214. result = FAIL;
  1215. }
  1216. if(BufTmp!=NULL)
  1217. free(BufTmp);
  1218. return result;
  1219. }
  1220. void getConnectorInfo(uint8_t gun_index, uint8_t *outBuffer)
  1221. {
  1222. uint8_t idxAC = 0;
  1223. uint8_t idxCCS = 0;
  1224. uint8_t idxCHAdeMO = 0;
  1225. uint8_t idxGBT = 0;
  1226. uint8_t AC_GUN_IDX = 0;
  1227. DEBUG_INFO("ModelName: %s\r\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  1228. DEBUG_INFO("gun_index : %d...\r\n", gun_index);
  1229. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1230. {
  1231. if(gun_index == 2)
  1232. {
  1233. AC_GUN_IDX = 0;
  1234. }
  1235. else if(gun_index == 1)
  1236. {
  1237. AC_GUN_IDX = 1;
  1238. }
  1239. else if(gun_index == 0)
  1240. {
  1241. AC_GUN_IDX = 2;
  1242. }
  1243. DEBUG_INFO("AC_GUN_IDX : %d...\r\n",AC_GUN_IDX);
  1244. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1245. {
  1246. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1247. {
  1248. case '1' ... '6':
  1249. idxAC++;
  1250. break;
  1251. }
  1252. DEBUG_INFO("idx : %d...\r\n",idx);
  1253. }
  1254. DEBUG_INFO("idxAC : %d...\r\n",idxAC);
  1255. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1256. {
  1257. case '1' ... '6':
  1258. // AC
  1259. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus;
  1260. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>8)&0xff;
  1261. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>0)&0xff;
  1262. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>8)&0xff;
  1263. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>0)&0xff;
  1264. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>8)&0xff;
  1265. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>0)&0xff;
  1266. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>8)&0xff;
  1267. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>0)&0xff;
  1268. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>8)&0xff;
  1269. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>0)&0xff;
  1270. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>8)&0xff;
  1271. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>0)&0xff;
  1272. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].EvBatterySoc)>>0)&0xff;
  1273. break;
  1274. default:
  1275. break;
  1276. }
  1277. }
  1278. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1279. {
  1280. for(uint8_t idx=0;idx<gun_index;idx++)
  1281. {
  1282. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1283. {
  1284. case '1' ... '6':
  1285. idxAC++;
  1286. break;
  1287. case 'J':
  1288. idxCHAdeMO++;
  1289. break;
  1290. case 'U':
  1291. case 'E':
  1292. idxCCS++;
  1293. break;
  1294. case 'G':
  1295. idxGBT++;
  1296. break;
  1297. }
  1298. }
  1299. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1300. {
  1301. case '1' ... '6':
  1302. // AC
  1303. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus;
  1304. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>8)&0xff;
  1305. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>0)&0xff;
  1306. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>8)&0xff;
  1307. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>0)&0xff;
  1308. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>8)&0xff;
  1309. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>0)&0xff;
  1310. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>8)&0xff;
  1311. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>0)&0xff;
  1312. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>8)&0xff;
  1313. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>0)&0xff;
  1314. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>8)&0xff;
  1315. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>0)&0xff;
  1316. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].EvBatterySoc)>>0)&0xff;
  1317. break;
  1318. case 'J':
  1319. // CHAdeMO
  1320. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus;
  1321. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingVoltage*10)>>8)&0xff;
  1322. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingVoltage*10)>>0)&0xff;
  1323. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingCurrent*10)>>8)&0xff;
  1324. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingCurrent*10)>>0)&0xff;
  1325. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingPower*10)>>8)&0xff;
  1326. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingPower*10)>>0)&0xff;
  1327. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedEnergy*10)>>8)&0xff;
  1328. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedEnergy*10)>>0)&0xff;
  1329. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedDuration)>>8)&0xff;
  1330. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedDuration)>>0)&0xff;
  1331. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].RemainChargingDuration)>>8)&0xff;
  1332. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].RemainChargingDuration)>>0)&0xff;
  1333. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].EvBatterySoc)>>0)&0xff;
  1334. break;
  1335. case 'U':
  1336. case 'E':
  1337. // CCS
  1338. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus;
  1339. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingVoltage*10)>>8)&0xff;
  1340. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingVoltage*10)>>0)&0xff;
  1341. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingCurrent*10)>>8)&0xff;
  1342. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingCurrent*10)>>0)&0xff;
  1343. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingPower*10)>>8)&0xff;
  1344. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingPower*10)>>0)&0xff;
  1345. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedEnergy*10)>>8)&0xff;
  1346. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedEnergy*10)>>0)&0xff;
  1347. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedDuration)>>8)&0xff;
  1348. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedDuration)>>0)&0xff;
  1349. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].RemainChargingDuration)>>8)&0xff;
  1350. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].RemainChargingDuration)>>0)&0xff;
  1351. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].EvBatterySoc)>>0)&0xff;
  1352. break;
  1353. case 'G':
  1354. // GBT
  1355. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus;
  1356. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingVoltage*10)>>8)&0xff;
  1357. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingVoltage*10)>>0)&0xff;
  1358. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingCurrent*10)>>8)&0xff;
  1359. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingCurrent*10)>>0)&0xff;
  1360. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingPower*10)>>8)&0xff;
  1361. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingPower*10)>>0)&0xff;
  1362. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedEnergy*10)>>8)&0xff;
  1363. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedEnergy*10)>>0)&0xff;
  1364. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedDuration)>>8)&0xff;
  1365. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedDuration)>>0)&0xff;
  1366. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].RemainChargingDuration)>>8)&0xff;
  1367. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].RemainChargingDuration)>>0)&0xff;
  1368. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].EvBatterySoc)>>0)&0xff;
  1369. break;
  1370. default:
  1371. break;
  1372. }
  1373. }
  1374. }
  1375. void getConnectorSchedule(uint8_t gun_index, uint8_t *outBuffer)
  1376. {
  1377. uint8_t idxAC = 0;
  1378. uint8_t idxCCS = 0;
  1379. uint8_t idxCHAdeMO = 0;
  1380. uint8_t idxGBT = 0;
  1381. uint8_t AC_GUN_IDX = 0;
  1382. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1383. {
  1384. if(gun_index == 2)
  1385. {
  1386. AC_GUN_IDX = 0;
  1387. }
  1388. else if(gun_index == 1)
  1389. {
  1390. AC_GUN_IDX = 1;
  1391. }
  1392. else if(gun_index == 0)
  1393. {
  1394. AC_GUN_IDX = 2;
  1395. }
  1396. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1397. {
  1398. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1399. {
  1400. case '1' ... '6':
  1401. idxAC++;
  1402. break;
  1403. }
  1404. }
  1405. }
  1406. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1407. {
  1408. for(uint8_t idx=0;idx<gun_index;idx++)
  1409. {
  1410. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1411. {
  1412. case '1' ... '6':
  1413. idxAC++;
  1414. break;
  1415. case 'J':
  1416. idxCHAdeMO++;
  1417. break;
  1418. case 'U':
  1419. case 'E':
  1420. idxCCS++;
  1421. break;
  1422. case 'G':
  1423. idxGBT++;
  1424. break;
  1425. }
  1426. }
  1427. }
  1428. /*
  1429. * TODO:
  1430. * 1. Connector schedule get
  1431. */
  1432. }
  1433. uint8_t startConnectorCharging(uint8_t gun_index)
  1434. {
  1435. uint8_t idxAC = 0;
  1436. uint8_t idxCCS = 0;
  1437. uint8_t idxCHAdeMO = 0;
  1438. uint8_t idxGBT = 0;
  1439. uint8_t result = OFF;
  1440. uint8_t AC_GUN_IDX = 0;
  1441. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1442. {
  1443. if(gun_index == 2)
  1444. {
  1445. AC_GUN_IDX = 0;
  1446. }
  1447. else if(gun_index == 1)
  1448. {
  1449. AC_GUN_IDX = 1;
  1450. }
  1451. else if(gun_index == 0)
  1452. {
  1453. AC_GUN_IDX = 2;
  1454. }
  1455. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1456. {
  1457. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1458. {
  1459. case '1' ... '6':
  1460. idxAC++;
  1461. break;
  1462. }
  1463. }
  1464. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1465. {
  1466. case '1' ... '6':
  1467. // AC
  1468. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)
  1469. {
  1470. ShmOCPP16Data->CsMsg.bits[AC_GUN_IDX].RemoteStartTransactionReq = ON;
  1471. result = ON;
  1472. }
  1473. break;
  1474. default:
  1475. break;
  1476. }
  1477. return result;
  1478. }
  1479. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1480. {
  1481. for(uint8_t idx=0;idx<gun_index;idx++)
  1482. {
  1483. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1484. {
  1485. case '1' ... '6':
  1486. idxAC++;
  1487. break;
  1488. case 'J':
  1489. idxCHAdeMO++;
  1490. break;
  1491. case 'U':
  1492. case 'E':
  1493. idxCCS++;
  1494. break;
  1495. case 'G':
  1496. idxGBT++;
  1497. break;
  1498. }
  1499. }
  1500. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1501. {
  1502. case '1' ... '6':
  1503. // AC
  1504. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)
  1505. {
  1506. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1507. DEBUG_INFO("AC : %d...\r\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1508. result = ON;
  1509. }
  1510. break;
  1511. case 'J':
  1512. // CHAdeMO
  1513. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus == SYS_MODE_IDLE)
  1514. {
  1515. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1516. DEBUG_INFO("CHAdeMO : %d...\r\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1517. result = ON;
  1518. }
  1519. break;
  1520. case 'U':
  1521. case 'E':
  1522. // CCS
  1523. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus == SYS_MODE_IDLE)
  1524. {
  1525. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1526. DEBUG_INFO("CCS : %d...\r\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1527. result = ON;
  1528. }
  1529. break;
  1530. case 'G':
  1531. // GBT
  1532. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus == SYS_MODE_IDLE)
  1533. {
  1534. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1535. DEBUG_INFO("GBT : %d...\r\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1536. result = ON;
  1537. }
  1538. break;
  1539. default:
  1540. break;
  1541. }
  1542. }
  1543. return result;
  1544. }
  1545. uint8_t stopConnectorCharging(uint8_t gun_index)
  1546. {
  1547. uint8_t idxAC = 0;
  1548. uint8_t idxCCS = 0;
  1549. uint8_t idxCHAdeMO = 0;
  1550. uint8_t idxGBT = 0;
  1551. uint8_t result = OFF;
  1552. uint8_t AC_GUN_IDX = 0;
  1553. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1554. {
  1555. if(gun_index == 2)
  1556. {
  1557. AC_GUN_IDX = 0;
  1558. }
  1559. else if(gun_index == 1)
  1560. {
  1561. AC_GUN_IDX = 1;
  1562. }
  1563. else if(gun_index == 0)
  1564. {
  1565. AC_GUN_IDX = 2;
  1566. }
  1567. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1568. {
  1569. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1570. {
  1571. case '1' ... '6':
  1572. idxAC++;
  1573. break;
  1574. }
  1575. }
  1576. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1577. {
  1578. case '1' ... '6':
  1579. // AC
  1580. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus != SYS_MODE_IDLE)
  1581. {
  1582. ShmOCPP16Data->CsMsg.bits[AC_GUN_IDX].RemoteStopTransactionReq = ON;
  1583. result = ON;
  1584. }
  1585. break;
  1586. default:
  1587. break;
  1588. }
  1589. return result;
  1590. }
  1591. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1592. {
  1593. for(uint8_t idx=0;idx<gun_index;idx++)
  1594. {
  1595. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1596. {
  1597. case '1' ... '6':
  1598. idxAC++;
  1599. break;
  1600. case 'J':
  1601. idxCHAdeMO++;
  1602. break;
  1603. case 'U':
  1604. case 'E':
  1605. idxCCS++;
  1606. break;
  1607. case 'G':
  1608. idxGBT++;
  1609. break;
  1610. }
  1611. }
  1612. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1613. {
  1614. case '1' ... '6':
  1615. // AC
  1616. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus != SYS_MODE_IDLE)
  1617. {
  1618. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1619. result = ON;
  1620. }
  1621. break;
  1622. case 'J':
  1623. // CHAdeMO
  1624. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus != SYS_MODE_IDLE)
  1625. {
  1626. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1627. result = ON;
  1628. }
  1629. break;
  1630. case 'U':
  1631. case 'E':
  1632. // CCS
  1633. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus != SYS_MODE_IDLE)
  1634. {
  1635. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1636. result = ON;
  1637. }
  1638. break;
  1639. case 'G':
  1640. // GBT
  1641. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus != SYS_MODE_IDLE)
  1642. {
  1643. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1644. result = ON;
  1645. }
  1646. break;
  1647. default:
  1648. break;
  1649. }
  1650. }
  1651. return result;
  1652. }
  1653. void setConnectorSchedule(uint8_t gun_index, uint8_t *inputBuffer)
  1654. {
  1655. uint8_t idxAC = 0;
  1656. uint8_t idxCCS = 0;
  1657. uint8_t idxCHAdeMO = 0;
  1658. uint8_t idxGBT = 0;
  1659. uint8_t AC_GUN_IDX = 0;
  1660. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1661. {
  1662. if(gun_index == 2)
  1663. {
  1664. AC_GUN_IDX = 0;
  1665. }
  1666. else if(gun_index == 1)
  1667. {
  1668. AC_GUN_IDX = 1;
  1669. }
  1670. else if(gun_index == 0)
  1671. {
  1672. AC_GUN_IDX = 2;
  1673. }
  1674. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1675. {
  1676. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1677. {
  1678. case '1' ... '6':
  1679. idxAC++;
  1680. break;
  1681. }
  1682. }
  1683. }
  1684. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1685. {
  1686. for(uint8_t idx=0;idx<gun_index;idx++)
  1687. {
  1688. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1689. {
  1690. case '1' ... '6':
  1691. idxAC++;
  1692. break;
  1693. case 'J':
  1694. idxCHAdeMO++;
  1695. break;
  1696. case 'U':
  1697. case 'E':
  1698. idxCCS++;
  1699. break;
  1700. case 'G':
  1701. idxGBT++;
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. /*
  1707. * TODO:
  1708. * 1. Connector schedule configuration
  1709. */
  1710. }
  1711. void getConnectorUser(uint8_t gun_index, uint8_t *outBuffer)
  1712. {
  1713. uint8_t idxAC = 0;
  1714. uint8_t idxCCS = 0;
  1715. uint8_t idxCHAdeMO = 0;
  1716. uint8_t idxGBT = 0;
  1717. uint8_t AC_GUN_IDX = 0;
  1718. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1719. {
  1720. if(gun_index == 2)
  1721. {
  1722. AC_GUN_IDX = 0;
  1723. }
  1724. else if(gun_index == 1)
  1725. {
  1726. AC_GUN_IDX = 1;
  1727. }
  1728. else if(gun_index == 0)
  1729. {
  1730. AC_GUN_IDX = 2;
  1731. }
  1732. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1733. {
  1734. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1735. {
  1736. case '1' ... '6':
  1737. idxAC++;
  1738. break;
  1739. }
  1740. }
  1741. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1742. {
  1743. case '1' ... '6':
  1744. // AC
  1745. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1746. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId));
  1747. break;
  1748. default:
  1749. break;
  1750. }
  1751. }
  1752. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1753. {
  1754. for(uint8_t idx=0;idx<gun_index;idx++)
  1755. {
  1756. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1757. {
  1758. case '1' ... '6':
  1759. idxAC++;
  1760. break;
  1761. case 'J':
  1762. idxCHAdeMO++;
  1763. break;
  1764. case 'U':
  1765. case 'E':
  1766. idxCCS++;
  1767. break;
  1768. case 'G':
  1769. idxGBT++;
  1770. break;
  1771. }
  1772. }
  1773. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1774. {
  1775. case '1' ... '6':
  1776. // AC
  1777. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1778. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId));
  1779. break;
  1780. case 'J':
  1781. // CHAdeMO
  1782. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1783. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].StartUserId));
  1784. break;
  1785. case 'U':
  1786. case 'E':
  1787. // CCS
  1788. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1789. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].StartUserId));
  1790. break;
  1791. case 'G':
  1792. // GBT
  1793. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1794. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].StartUserId));
  1795. break;
  1796. default:
  1797. break;
  1798. }
  1799. }
  1800. }
  1801. int tcpSocketServerStart(void)
  1802. {
  1803. int sockFd = 0;
  1804. int clientSockFd = 0;
  1805. uint8_t inputBuffer[2048] = {};
  1806. uint8_t outBuffer[2048] = {};
  1807. int16_t read_size;
  1808. int16_t tx_size;
  1809. uint32_t randomChk;
  1810. struct sockaddr_in serverInfo, clientInfo;
  1811. socklen_t addrlen = sizeof(clientInfo);
  1812. uint8_t cmdBuf[512];
  1813. time_t rawtime;
  1814. struct tm ts;
  1815. sockFd = socket(AF_INET , SOCK_STREAM , 0);
  1816. if(sockFd == -1)
  1817. {
  1818. DEBUG_ERROR("InitSocketServer NG\n");
  1819. sleep(5);
  1820. return FAIL;
  1821. }
  1822. bzero(&serverInfo,sizeof(serverInfo));
  1823. serverInfo.sin_family = PF_INET;
  1824. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  1825. serverInfo.sin_port = htons(LISTEN_PORT_TCP);
  1826. bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
  1827. listen(sockFd, CONNECTION_LIMIT);
  1828. // Main loop
  1829. for(;;)
  1830. {
  1831. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  1832. DEBUG_INFO("Client connect in.\r\n");
  1833. DEBUG_INFO("clientSockFd : %d...\r\n", clientSockFd);
  1834. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  1835. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  1836. while((read_size = recv(clientSockFd, inputBuffer, sizeof(inputBuffer), 0)) > 0)
  1837. {
  1838. if(isValidCheckSum(inputBuffer) && (randomChk != ((inputBuffer[4]<<24) | (inputBuffer[5]<<16) | (inputBuffer[6]<<8) | (inputBuffer[7]<<0))))
  1839. {
  1840. randomChk = ((inputBuffer[4]<<24) | (inputBuffer[5]<<16) | (inputBuffer[6]<<8) | (inputBuffer[7]<<0));
  1841. memset(outBuffer, 0x00, sizeof(outBuffer));
  1842. switch(inputBuffer[8])
  1843. {
  1844. case WIFI_CMD_LOGIN_SIGNIN:
  1845. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  1846. memcpy((char*)&wifi_login_info.loginId[0], (char*)&inputBuffer[9], (((inputBuffer[2] << 8)| inputBuffer[3])-6));
  1847. wifi_login_info.loginRole = loginValid(localDb);
  1848. DEBUG_INFO("Login user id: %s\r\n", wifi_login_info.loginId);
  1849. DEBUG_INFO("User id: %d\r\n", wifi_login_info.loginRole);
  1850. tx_size = 7;
  1851. outBuffer[0] = 0xff;
  1852. outBuffer[1] = 0xff;
  1853. outBuffer[2] = 0x00;
  1854. outBuffer[3] = 0x03;
  1855. outBuffer[4] = WIFI_CMD_LOGIN_SIGNIN;
  1856. switch(wifi_login_info.loginRole)
  1857. {
  1858. case ROLE_MANAGER:
  1859. case ROLE_USER:
  1860. // Login success
  1861. outBuffer[5] = 0x02;
  1862. break;
  1863. case ROLE_UNKNOWN:
  1864. default:
  1865. if(!isPukReg(localDb))
  1866. {
  1867. // Login fail, PUK never be registered
  1868. outBuffer[5] = 0x01;
  1869. }
  1870. else
  1871. {
  1872. // Login fail
  1873. outBuffer[5] = 0x00;
  1874. }
  1875. break;
  1876. }
  1877. outBuffer[6] = chksumCal(outBuffer);
  1878. break;
  1879. case WIFI_CMD_LOGIN_AUTH_PUK:
  1880. if(!isPukReg(localDb))
  1881. {
  1882. // PUK never be registered
  1883. if((wifi_login_info.puk[0] == inputBuffer[9]) &&
  1884. (wifi_login_info.puk[1] == inputBuffer[10]) &&
  1885. (wifi_login_info.puk[2] == inputBuffer[11]) &&
  1886. (wifi_login_info.puk[3] == inputBuffer[12]) &&
  1887. (wifi_login_info.puk[4] == inputBuffer[13]) &&
  1888. (wifi_login_info.puk[5] == inputBuffer[14]) &&
  1889. (wifi_login_info.puk[6] == inputBuffer[15]) &&
  1890. (wifi_login_info.puk[7] == inputBuffer[16]))
  1891. {
  1892. // PUK valid success
  1893. if(PukReg(localDb))
  1894. {
  1895. // PUK registered success
  1896. tx_size = 7 + strlen((char*)wifi_login_info.loginId);
  1897. outBuffer[0] = 0xff;
  1898. outBuffer[1] = 0xff;
  1899. outBuffer[2] = ((0x03 + strlen((char*)wifi_login_info.loginId))>>8)&0xff;
  1900. outBuffer[3] = (0x03 + strlen((char*)wifi_login_info.loginId)) & 0xff;
  1901. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  1902. outBuffer[5] = 0x01;
  1903. memcpy(&outBuffer[6], wifi_login_info.loginId, strlen((char*)wifi_login_info.loginId));
  1904. outBuffer[6+strlen((char*)wifi_login_info.loginId)] = chksumCal(outBuffer);
  1905. }
  1906. else
  1907. {
  1908. // PUK registered fail
  1909. tx_size = 7;
  1910. outBuffer[0] = 0xff;
  1911. outBuffer[1] = 0xff;
  1912. outBuffer[2] = 0x00;
  1913. outBuffer[3] = 0x03;
  1914. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  1915. outBuffer[5] = 0x00;
  1916. outBuffer[6] = chksumCal(outBuffer);
  1917. }
  1918. }
  1919. else
  1920. {
  1921. // PUK valid fail
  1922. tx_size = 7;
  1923. outBuffer[0] = 0xff;
  1924. outBuffer[1] = 0xff;
  1925. outBuffer[2] = 0x00;
  1926. outBuffer[3] = 0x03;
  1927. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  1928. outBuffer[5] = 0x00;
  1929. outBuffer[6] = chksumCal(outBuffer);
  1930. }
  1931. }
  1932. else
  1933. {
  1934. // PUK already be registered
  1935. tx_size = 7;
  1936. outBuffer[0] = 0xff;
  1937. outBuffer[1] = 0xff;
  1938. outBuffer[2] = 0x00;
  1939. outBuffer[3] = 0x03;
  1940. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  1941. outBuffer[5] = 0x02;
  1942. outBuffer[6] = chksumCal(outBuffer);
  1943. }
  1944. break;
  1945. case WIFI_CMD_LOGIN_GET_USER_ID:
  1946. if(wifi_login_info.loginRole != ROLE_UNKNOWN)
  1947. {
  1948. // Login role is Manager or User
  1949. tx_size = 7 + strlen((char*)wifi_login_info.loginId);
  1950. outBuffer[0] = 0xff;
  1951. outBuffer[1] = 0xff;
  1952. outBuffer[2] = ((0x03 + strlen((char*)wifi_login_info.loginId))>>8)&0xff;
  1953. outBuffer[3] = (0x03 + strlen((char*)wifi_login_info.loginId)) & 0xff;
  1954. outBuffer[4] = WIFI_CMD_LOGIN_GET_USER_ID;
  1955. outBuffer[5] = 0x01;
  1956. memcpy(&outBuffer[6], wifi_login_info.loginId, strlen((char*)wifi_login_info.loginId));
  1957. outBuffer[6+strlen((char*)wifi_login_info.loginId)] = chksumCal(outBuffer);
  1958. }
  1959. else
  1960. {
  1961. // Login role is Unknown
  1962. tx_size = 7;
  1963. outBuffer[0] = 0xff;
  1964. outBuffer[1] = 0xff;
  1965. outBuffer[2] = 0x00;
  1966. outBuffer[3] = 0x03;
  1967. outBuffer[4] = WIFI_CMD_LOGIN_GET_USER_ID;
  1968. outBuffer[5] = 0x00;
  1969. outBuffer[6] = chksumCal(outBuffer);
  1970. }
  1971. break;
  1972. case WIFI_CMD_PROTOCOL_VERSION:
  1973. tx_size = 12;
  1974. outBuffer[0] = 0xff;
  1975. outBuffer[1] = 0xff;
  1976. outBuffer[2] = 0x00;
  1977. outBuffer[3] = 0x08;
  1978. outBuffer[4] = WIFI_CMD_PROTOCOL_VERSION;
  1979. outBuffer[5] = (protocol_Version[0] >> 0x00) & 0xff;
  1980. outBuffer[6] = (protocol_Version[0] >> 0x08) & 0xff;
  1981. outBuffer[7] = (protocol_Version[1] >> 0x00) & 0xff;
  1982. outBuffer[8] = (protocol_Version[1] >> 0x08) & 0xff;
  1983. outBuffer[9] = (protocol_Version[2] >> 0x00) & 0xff;
  1984. outBuffer[10] = (protocol_Version[2] >> 0x08) & 0xff;
  1985. outBuffer[11] = chksumCal(outBuffer);
  1986. break;
  1987. case WIFI_CMD_GET_MODEL_NAME:
  1988. tx_size = 70;
  1989. outBuffer[0] = 0xff;
  1990. outBuffer[1] = 0xff;
  1991. outBuffer[2] = 0x00;
  1992. outBuffer[3] = 0x42;
  1993. outBuffer[4] = WIFI_CMD_GET_MODEL_NAME;
  1994. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1995. outBuffer[69] = chksumCal(outBuffer);
  1996. break;
  1997. case WIFI_CMD_GET_SERIAL_NUMBER:
  1998. tx_size = 70;
  1999. outBuffer[0] = 0xff;
  2000. outBuffer[1] = 0xff;
  2001. outBuffer[2] = 0x00;
  2002. outBuffer[3] = 0x42;
  2003. outBuffer[4] = WIFI_CMD_GET_SERIAL_NUMBER;
  2004. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  2005. outBuffer[69] = chksumCal(outBuffer);
  2006. break;
  2007. case WIFI_CMD_GET_SYSTEM_TIME:
  2008. tx_size = 10;
  2009. outBuffer[0] = 0xff;
  2010. outBuffer[1] = 0xff;
  2011. outBuffer[2] = 0x00;
  2012. outBuffer[3] = 0x06;
  2013. outBuffer[4] = WIFI_CMD_GET_SYSTEM_TIME;
  2014. outBuffer[5] = (time(NULL)>>24)&0xff;
  2015. outBuffer[6] = (time(NULL)>>16)&0xff;
  2016. outBuffer[7] = (time(NULL)>>8)&0xff;
  2017. outBuffer[8] = (time(NULL)>>0)&0xff;
  2018. outBuffer[9] = chksumCal(outBuffer);
  2019. break;
  2020. case WIFI_CMD_GET_WHITE_CARD:
  2021. tx_size = 40;
  2022. outBuffer[0] = 0xff;
  2023. outBuffer[1] = 0xff;
  2024. outBuffer[2] = 0x00;
  2025. outBuffer[3] = 0x24;
  2026. outBuffer[4] = WIFI_CMD_GET_WHITE_CARD;
  2027. outBuffer[5] = inputBuffer[9];
  2028. outBuffer[6] = ((strlen((char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]])>0)?0x00:0x01);
  2029. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]], ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]]));
  2030. outBuffer[39] = chksumCal(outBuffer);
  2031. break;
  2032. case WIFI_CMD_GET_SYS_ALARM_CODE:
  2033. tx_size = 70;
  2034. outBuffer[0] = 0xff;
  2035. outBuffer[1] = 0xff;
  2036. outBuffer[2] = 0x00;
  2037. outBuffer[3] = 0x42;
  2038. outBuffer[4] = WIFI_CMD_GET_SYS_ALARM_CODE;
  2039. memcpy(&outBuffer[5], ShmStatusCodeData->FaultCode.FaultEvents.FaultVal, ARRAY_SIZE(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal));
  2040. memcpy(&outBuffer[13], ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal, ARRAY_SIZE(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal));
  2041. memcpy(&outBuffer[29], ShmStatusCodeData->InfoCode.InfoEvents.InfoVal, ARRAY_SIZE(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal));
  2042. outBuffer[69] = chksumCal(outBuffer);
  2043. break;
  2044. case WIFI_CMD_GET_SYS_ID:
  2045. tx_size = 70;
  2046. outBuffer[0] = 0xff;
  2047. outBuffer[1] = 0xff;
  2048. outBuffer[2] = 0x00;
  2049. outBuffer[3] = 0x42;
  2050. outBuffer[4] = WIFI_CMD_GET_SYS_ID;
  2051. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.SystemId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  2052. outBuffer[69] = chksumCal(outBuffer);
  2053. break;
  2054. case WIFI_CMD_GET_RATING_CURRENT:
  2055. tx_size = 10;
  2056. outBuffer[0] = 0xff;
  2057. outBuffer[1] = 0xff;
  2058. outBuffer[2] = 0x00;
  2059. outBuffer[3] = 0x06;
  2060. outBuffer[4] = WIFI_CMD_GET_RATING_CURRENT;
  2061. outBuffer[5] = (ShmSysConfigAndInfo->SysConfig.RatingCurrent>>0x08) & 0xff;
  2062. outBuffer[6] = (ShmSysConfigAndInfo->SysConfig.RatingCurrent>>0x00) & 0xff;
  2063. outBuffer[7] = (ShmSysConfigAndInfo->SysConfig.AcRatingCurrent>>0x08) & 0xff;
  2064. outBuffer[8] = (ShmSysConfigAndInfo->SysConfig.AcRatingCurrent>>0x00) & 0xff;
  2065. outBuffer[9] = chksumCal(outBuffer);
  2066. break;
  2067. case WIFI_CMD_GET_VER_RFS:
  2068. tx_size = 38;
  2069. outBuffer[0] = 0xff;
  2070. outBuffer[1] = 0xff;
  2071. outBuffer[2] = 0x00;
  2072. outBuffer[3] = 0x22;
  2073. outBuffer[4] = WIFI_CMD_GET_VER_RFS;
  2074. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev));
  2075. outBuffer[37] = chksumCal(outBuffer);
  2076. break;
  2077. case WIFI_CMD_GET_VER_PRIMARY_CSU:
  2078. tx_size = 38;
  2079. outBuffer[0] = 0xff;
  2080. outBuffer[1] = 0xff;
  2081. outBuffer[2] = 0x00;
  2082. outBuffer[3] = 0x22;
  2083. outBuffer[4] = WIFI_CMD_GET_VER_PRIMARY_CSU;
  2084. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev));
  2085. outBuffer[37] = chksumCal(outBuffer);
  2086. break;
  2087. case WIFI_CMD_GET_VER_LCM:
  2088. tx_size = 38;
  2089. outBuffer[0] = 0xff;
  2090. outBuffer[1] = 0xff;
  2091. outBuffer[2] = 0x00;
  2092. outBuffer[3] = 0x22;
  2093. outBuffer[4] = WIFI_CMD_GET_VER_LCM;
  2094. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.LcmHwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.LcmHwRev));
  2095. outBuffer[37] = chksumCal(outBuffer);
  2096. break;
  2097. case WIFI_CMD_GET_PSU_COUNT:
  2098. tx_size = 7;
  2099. outBuffer[0] = 0xff;
  2100. outBuffer[1] = 0xff;
  2101. outBuffer[2] = 0x00;
  2102. outBuffer[3] = 0x03;
  2103. outBuffer[4] = WIFI_CMD_GET_PSU_COUNT;
  2104. outBuffer[5] = ShmPsuData->SystemPresentPsuQuantity;
  2105. outBuffer[6] = chksumCal(outBuffer);
  2106. break;
  2107. case WIFI_CMD_GET_VER_PSU:
  2108. tx_size = 39;
  2109. outBuffer[0] = 0xff;
  2110. outBuffer[1] = 0xff;
  2111. outBuffer[2] = 0x00;
  2112. outBuffer[3] = 0x23;
  2113. outBuffer[4] = WIFI_CMD_GET_VER_PSU;
  2114. outBuffer[5] = inputBuffer[9];
  2115. memcpy(&outBuffer[6], ShmPsuData->PsuVersion[inputBuffer[9]].FwPrimaryVersion, ARRAY_SIZE(ShmPsuData->PsuVersion[inputBuffer[9]].FwPrimaryVersion));
  2116. memcpy(&outBuffer[22], ShmPsuData->PsuVersion[inputBuffer[9]].FwSecondVersion, ARRAY_SIZE(ShmPsuData->PsuVersion[inputBuffer[9]].FwSecondVersion));
  2117. outBuffer[38] = chksumCal(outBuffer);
  2118. break;
  2119. case WIFI_CMD_GET_VER_AUX_POWER:
  2120. tx_size = 38;
  2121. outBuffer[0] = 0xff;
  2122. outBuffer[1] = 0xff;
  2123. outBuffer[2] = 0x00;
  2124. outBuffer[3] = 0x22;
  2125. outBuffer[4] = WIFI_CMD_GET_VER_AUX_POWER;
  2126. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev));
  2127. outBuffer[37] = chksumCal(outBuffer);
  2128. break;
  2129. case WIFI_CMD_GET_VER_FAN:
  2130. tx_size = 38;
  2131. outBuffer[0] = 0xff;
  2132. outBuffer[1] = 0xff;
  2133. outBuffer[2] = 0x00;
  2134. outBuffer[3] = 0x22;
  2135. outBuffer[4] = WIFI_CMD_GET_VER_FAN;
  2136. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  2137. outBuffer[37] = chksumCal(outBuffer);
  2138. break;
  2139. case WIFI_CMD_GET_VER_RELAY:
  2140. tx_size = 38;
  2141. outBuffer[0] = 0xff;
  2142. outBuffer[1] = 0xff;
  2143. outBuffer[2] = 0x00;
  2144. outBuffer[3] = 0x22;
  2145. outBuffer[4] = WIFI_CMD_GET_VER_RELAY;
  2146. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  2147. outBuffer[37] = chksumCal(outBuffer);
  2148. break;
  2149. case WIFI_CMD_GET_VER_TELECOMM:
  2150. tx_size = 38;
  2151. outBuffer[0] = 0xff;
  2152. outBuffer[1] = 0xff;
  2153. outBuffer[2] = 0x00;
  2154. outBuffer[3] = 0x22;
  2155. outBuffer[4] = WIFI_CMD_GET_VER_TELECOMM;
  2156. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer));
  2157. outBuffer[37] = chksumCal(outBuffer);
  2158. break;
  2159. case WIFI_CMD_GET_EVSE_CONFIG:
  2160. tx_size = 16;
  2161. outBuffer[0] = 0xff;
  2162. outBuffer[1] = 0xff;
  2163. outBuffer[2] = 0x00;
  2164. outBuffer[3] = 0x0C;
  2165. outBuffer[4] = WIFI_CMD_GET_EVSE_CONFIG;
  2166. outBuffer[5] = (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy>>0x08) & 0xff;
  2167. outBuffer[6] = (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy>>0x00) & 0xff;
  2168. outBuffer[7] = (ShmSysConfigAndInfo->SysConfig.MaxChargingPower>>0x08) & 0xff;
  2169. outBuffer[8] = (ShmSysConfigAndInfo->SysConfig.MaxChargingPower>>0x00) & 0xff;
  2170. outBuffer[9] = (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent>>0x08) & 0xff;
  2171. outBuffer[10] = (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent>>0x00) & 0xff;
  2172. outBuffer[11] = (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration>>0x08) & 0xff;
  2173. outBuffer[12] = (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration>>0x00) & 0xff;
  2174. outBuffer[13] = (ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent>>0x08) & 0xff;
  2175. outBuffer[14] = (ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent>>0x00) & 0xff;
  2176. outBuffer[15] = chksumCal(outBuffer);
  2177. break;
  2178. case WIFI_CMD_GET_GUN_STS:
  2179. tx_size = 21;
  2180. outBuffer[0] = 0xff;
  2181. outBuffer[1] = 0xff;
  2182. outBuffer[2] = 0x00;
  2183. outBuffer[3] = 0x11;
  2184. outBuffer[4] = WIFI_CMD_GET_GUN_STS;
  2185. outBuffer[5] = inputBuffer[9];
  2186. getConnectorInfo(inputBuffer[9], outBuffer);
  2187. outBuffer[20] = chksumCal(outBuffer);
  2188. break;
  2189. case WIFI_CMD_GET_GUN_USER:
  2190. tx_size = 40;
  2191. outBuffer[0] = 0xff;
  2192. outBuffer[1] = 0xff;
  2193. outBuffer[2] = 0x00;
  2194. outBuffer[3] = 0x24;
  2195. outBuffer[4] = WIFI_CMD_GET_GUN_USER;
  2196. outBuffer[5] = inputBuffer[9];
  2197. getConnectorUser(inputBuffer[9], outBuffer);
  2198. outBuffer[39] = chksumCal(outBuffer);
  2199. break;
  2200. case WIFI_CMD_GET_SCHEDULE:
  2201. tx_size = 10;
  2202. outBuffer[0] = 0xff;
  2203. outBuffer[1] = 0xff;
  2204. outBuffer[2] = 0x00;
  2205. outBuffer[3] = 0x06;
  2206. outBuffer[4] = WIFI_CMD_GET_SCHEDULE;
  2207. outBuffer[5] = inputBuffer[9];
  2208. getConnectorSchedule(inputBuffer[9], outBuffer);
  2209. outBuffer[9] = chksumCal(outBuffer);
  2210. break;
  2211. case WIFI_CMD_GET_NETWORK_STS:
  2212. tx_size = 7;
  2213. outBuffer[0] = 0xff;
  2214. outBuffer[1] = 0xff;
  2215. outBuffer[2] = 0x00;
  2216. outBuffer[3] = 0x03;
  2217. outBuffer[4] = WIFI_CMD_GET_NETWORK_STS;
  2218. outBuffer[5] = ShmSysConfigAndInfo->SysInfo.InternetConn;
  2219. outBuffer[6] = chksumCal(outBuffer);
  2220. break;
  2221. case WIFI_CMD_GET_ETH_DHCP:
  2222. tx_size = 7;
  2223. outBuffer[0] = 0xff;
  2224. outBuffer[1] = 0xff;
  2225. outBuffer[2] = 0x00;
  2226. outBuffer[3] = 0x03;
  2227. outBuffer[4] = WIFI_CMD_GET_ETH_DHCP;
  2228. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient^1;
  2229. outBuffer[6] = chksumCal(outBuffer);
  2230. break;
  2231. case WIFI_CMD_GET_ETH_MAC:
  2232. tx_size = 24;
  2233. outBuffer[0] = 0xff;
  2234. outBuffer[1] = 0xff;
  2235. outBuffer[2] = 0x00;
  2236. outBuffer[3] = 0x14;
  2237. outBuffer[4] = WIFI_CMD_GET_ETH_MAC;
  2238. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress));
  2239. outBuffer[23] = chksumCal(outBuffer);
  2240. break;
  2241. case WIFI_CMD_GET_ETH_IP:
  2242. tx_size = 22;
  2243. outBuffer[0] = 0xff;
  2244. outBuffer[1] = 0xff;
  2245. outBuffer[2] = 0x00;
  2246. outBuffer[3] = 0x12;
  2247. outBuffer[4] = WIFI_CMD_GET_ETH_IP;
  2248. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress));
  2249. outBuffer[21] = chksumCal(outBuffer);
  2250. break;
  2251. case WIFI_CMD_GET_ETH_SUBMASK:
  2252. tx_size = 22;
  2253. outBuffer[0] = 0xff;
  2254. outBuffer[1] = 0xff;
  2255. outBuffer[2] = 0x00;
  2256. outBuffer[3] = 0x12;
  2257. outBuffer[4] = WIFI_CMD_GET_ETH_SUBMASK;
  2258. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress));
  2259. outBuffer[21] = chksumCal(outBuffer);
  2260. break;
  2261. case WIFI_CMD_GET_ETH_GATEWAY:
  2262. tx_size = 22;
  2263. outBuffer[0] = 0xff;
  2264. outBuffer[1] = 0xff;
  2265. outBuffer[2] = 0x00;
  2266. outBuffer[3] = 0x12;
  2267. outBuffer[4] = WIFI_CMD_GET_ETH_GATEWAY;
  2268. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress));
  2269. outBuffer[21] = chksumCal(outBuffer);
  2270. break;
  2271. case WIFI_CMD_GET_WIFI_MODE:
  2272. tx_size = 7;
  2273. outBuffer[0] = 0xff;
  2274. outBuffer[1] = 0xff;
  2275. outBuffer[2] = 0x00;
  2276. outBuffer[3] = 0x03;
  2277. outBuffer[4] = WIFI_CMD_GET_WIFI_MODE;
  2278. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode;
  2279. outBuffer[6] = chksumCal(outBuffer);
  2280. break;
  2281. case WIFI_CMD_GET_WIFI_SSID:
  2282. tx_size = 261;
  2283. outBuffer[0] = 0xff;
  2284. outBuffer[1] = 0xff;
  2285. outBuffer[2] = 0x01;
  2286. outBuffer[3] = 0x01;
  2287. outBuffer[4] = WIFI_CMD_GET_WIFI_SSID;
  2288. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid));
  2289. outBuffer[260] = chksumCal(outBuffer);
  2290. break;
  2291. case WIFI_CMD_GET_WIFI_PASSWD:
  2292. tx_size = 261;
  2293. outBuffer[0] = 0xff;
  2294. outBuffer[1] = 0xff;
  2295. outBuffer[2] = 0x01;
  2296. outBuffer[3] = 0x01;
  2297. outBuffer[4] = WIFI_CMD_GET_WIFI_PASSWD;
  2298. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword));
  2299. outBuffer[260] = chksumCal(outBuffer);
  2300. break;
  2301. case WIFI_CMD_GET_WIFI_RSSI:
  2302. tx_size = 7;
  2303. outBuffer[0] = 0xff;
  2304. outBuffer[1] = 0xff;
  2305. outBuffer[2] = 0x00;
  2306. outBuffer[3] = 0x03;
  2307. outBuffer[4] = WIFI_CMD_GET_WIFI_RSSI;
  2308. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi+100;
  2309. outBuffer[6] = chksumCal(outBuffer);
  2310. break;
  2311. case WIFI_CMD_GET_WIFI_DHCP:
  2312. tx_size = 7;
  2313. outBuffer[0] = 0xff;
  2314. outBuffer[1] = 0xff;
  2315. outBuffer[2] = 0x00;
  2316. outBuffer[3] = 0x03;
  2317. outBuffer[4] = WIFI_CMD_GET_WIFI_DHCP;
  2318. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient^1;
  2319. outBuffer[6] = chksumCal(outBuffer);
  2320. break;
  2321. case WIFI_CMD_GET_WIFI_MAC:
  2322. tx_size = 24;
  2323. outBuffer[0] = 0xff;
  2324. outBuffer[1] = 0xff;
  2325. outBuffer[2] = 0x00;
  2326. outBuffer[3] = 0x14;
  2327. outBuffer[4] = WIFI_CMD_GET_WIFI_MAC;
  2328. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress));
  2329. outBuffer[23] = chksumCal(outBuffer);
  2330. break;
  2331. case WIFI_CMD_GET_WIFI_IP:
  2332. tx_size = 22;
  2333. outBuffer[0] = 0xff;
  2334. outBuffer[1] = 0xff;
  2335. outBuffer[2] = 0x00;
  2336. outBuffer[3] = 0x12;
  2337. outBuffer[4] = WIFI_CMD_GET_WIFI_IP;
  2338. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress));
  2339. outBuffer[21] = chksumCal(outBuffer);
  2340. break;
  2341. case WIFI_CMD_GET_WIFI_NETMASK:
  2342. tx_size = 22;
  2343. outBuffer[0] = 0xff;
  2344. outBuffer[1] = 0xff;
  2345. outBuffer[2] = 0x00;
  2346. outBuffer[3] = 0x12;
  2347. outBuffer[4] = WIFI_CMD_GET_WIFI_NETMASK;
  2348. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress));
  2349. outBuffer[21] = chksumCal(outBuffer);
  2350. break;
  2351. case WIFI_CMD_GET_WIFI_GATEWAY:
  2352. tx_size = 22;
  2353. outBuffer[0] = 0xff;
  2354. outBuffer[1] = 0xff;
  2355. outBuffer[2] = 0x00;
  2356. outBuffer[3] = 0x12;
  2357. outBuffer[4] = WIFI_CMD_GET_WIFI_GATEWAY;
  2358. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress));
  2359. outBuffer[21] = chksumCal(outBuffer);
  2360. break;
  2361. case WIFI_CMD_GET_TELE_APN:
  2362. tx_size = 262;
  2363. outBuffer[0] = 0xff;
  2364. outBuffer[1] = 0xff;
  2365. outBuffer[2] = 0x01;
  2366. outBuffer[3] = 0x02;
  2367. outBuffer[4] = WIFI_CMD_GET_TELE_APN;
  2368. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn));
  2369. outBuffer[261] = chksumCal(outBuffer);
  2370. break;
  2371. case WIFI_CMD_GET_TELE_RSSI:
  2372. tx_size = 7;
  2373. outBuffer[0] = 0xff;
  2374. outBuffer[1] = 0xff;
  2375. outBuffer[2] = 0x00;
  2376. outBuffer[3] = 0x03;
  2377. outBuffer[4] = WIFI_CMD_GET_TELE_RSSI;
  2378. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi+100;
  2379. outBuffer[6] = chksumCal(outBuffer);
  2380. break;
  2381. case WIFI_CMD_GET_TELE_CHAP_ID:
  2382. tx_size = 262;
  2383. outBuffer[0] = 0xff;
  2384. outBuffer[1] = 0xff;
  2385. outBuffer[2] = 0x01;
  2386. outBuffer[3] = 0x02;
  2387. outBuffer[4] = WIFI_CMD_GET_TELE_CHAP_ID;
  2388. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId));
  2389. outBuffer[261] = chksumCal(outBuffer);
  2390. break;
  2391. case WIFI_CMD_GET_TELE_CHAP_PASSWD:
  2392. tx_size = 262;
  2393. outBuffer[0] = 0xff;
  2394. outBuffer[1] = 0xff;
  2395. outBuffer[2] = 0x01;
  2396. outBuffer[3] = 0x02;
  2397. outBuffer[4] = WIFI_CMD_GET_TELE_CHAP_PASSWD;
  2398. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd));
  2399. outBuffer[261] = chksumCal(outBuffer);
  2400. break;
  2401. case WIFI_CMD_GET_TELE_IMEI:
  2402. tx_size = 22;
  2403. outBuffer[0] = 0xff;
  2404. outBuffer[1] = 0xff;
  2405. outBuffer[2] = 0x00;
  2406. outBuffer[3] = 0x12;
  2407. outBuffer[4] = WIFI_CMD_GET_TELE_IMEI;
  2408. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei));
  2409. outBuffer[21] = chksumCal(outBuffer);
  2410. break;
  2411. case WIFI_CMD_GET_TELE_IMSI:
  2412. tx_size = 22;
  2413. outBuffer[0] = 0xff;
  2414. outBuffer[1] = 0xff;
  2415. outBuffer[2] = 0x00;
  2416. outBuffer[3] = 0x12;
  2417. outBuffer[4] = WIFI_CMD_GET_TELE_IMSI;
  2418. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi));
  2419. outBuffer[21] = chksumCal(outBuffer);
  2420. break;
  2421. case WIFI_CMD_GET_TELE_SIM_STS:
  2422. tx_size = 7;
  2423. outBuffer[0] = 0xff;
  2424. outBuffer[1] = 0xff;
  2425. outBuffer[2] = 0x00;
  2426. outBuffer[3] = 0x03;
  2427. outBuffer[4] = WIFI_CMD_GET_TELE_SIM_STS;
  2428. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus;
  2429. outBuffer[6] = chksumCal(outBuffer);
  2430. break;
  2431. case WIFI_CMD_GET_TELE_MODE:
  2432. tx_size = 7;
  2433. outBuffer[0] = 0xff;
  2434. outBuffer[1] = 0xff;
  2435. outBuffer[2] = 0x00;
  2436. outBuffer[3] = 0x03;
  2437. outBuffer[4] = WIFI_CMD_GET_TELE_MODE;
  2438. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode;
  2439. outBuffer[6] = chksumCal(outBuffer);
  2440. break;
  2441. case WIFI_CMD_GET_TELE_IP:
  2442. tx_size = 22;
  2443. outBuffer[0] = 0xff;
  2444. outBuffer[1] = 0xff;
  2445. outBuffer[2] = 0x00;
  2446. outBuffer[3] = 0x12;
  2447. outBuffer[4] = WIFI_CMD_GET_TELE_IP;
  2448. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress));
  2449. outBuffer[21] = chksumCal(outBuffer);
  2450. break;
  2451. case WIFI_CMD_SET_SYSTEM_TIME:
  2452. rawtime = (inputBuffer[9]<<0) | (inputBuffer[10]<<8) | (inputBuffer[11]<<16) |(inputBuffer[12]<<24);
  2453. ts = *localtime(&rawtime);
  2454. sprintf((char*)cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", (ts.tm_year+1900),
  2455. (ts.tm_mon+1),
  2456. ts.tm_mday,
  2457. ts.tm_hour,
  2458. ts.tm_min,
  2459. ts.tm_sec);
  2460. system((char*)cmdBuf);
  2461. system("hwclock -w -u");
  2462. system("hwclock -s");
  2463. tx_size = 7;
  2464. outBuffer[0] = 0xff;
  2465. outBuffer[1] = 0xff;
  2466. outBuffer[2] = 0x00;
  2467. outBuffer[3] = 0x03;
  2468. outBuffer[4] = WIFI_CMD_SET_SYSTEM_TIME;
  2469. outBuffer[5] = 0x01;
  2470. outBuffer[6] = chksumCal(outBuffer);
  2471. break;
  2472. case WIFI_CMD_SET_WHITE_CARD:
  2473. if((0<=inputBuffer[9]) && (inputBuffer[9]<10))
  2474. {
  2475. if(inputBuffer[10] == REGISTER)
  2476. {
  2477. // Register white card
  2478. memcpy(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]], &inputBuffer[11],(((inputBuffer[2] << 8) | inputBuffer[3])-8));
  2479. }
  2480. else if (inputBuffer[10] == CLEAR_REGISTER)
  2481. {
  2482. // Clear register white card
  2483. memset(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]], 0x00, ARRAY_SIZE(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]]));
  2484. }
  2485. outBuffer[6] = 0x01;
  2486. }
  2487. else
  2488. {
  2489. outBuffer[6] = 0x00;
  2490. }
  2491. tx_size = 8;
  2492. outBuffer[0] = 0xff;
  2493. outBuffer[1] = 0xff;
  2494. outBuffer[2] = 0x00;
  2495. outBuffer[3] = 0x04;
  2496. outBuffer[4] = WIFI_CMD_SET_WHITE_CARD;
  2497. outBuffer[5] = inputBuffer[9];
  2498. outBuffer[7] = chksumCal(outBuffer);
  2499. break;
  2500. case WIFI_CMD_SET_FACTORY_RESET:
  2501. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = ON;
  2502. tx_size = 7;
  2503. outBuffer[0] = 0xff;
  2504. outBuffer[1] = 0xff;
  2505. outBuffer[2] = 0x00;
  2506. outBuffer[3] = 0x03;
  2507. outBuffer[4] = WIFI_CMD_SET_FACTORY_RESET;
  2508. outBuffer[5] = 0x01;
  2509. outBuffer[6] = chksumCal(outBuffer);
  2510. break;
  2511. case WIFI_CMD_SET_EVSE_CONFIG:
  2512. if((inputBuffer[9]>>4)&0x01)
  2513. {
  2514. SysConfigTemporarily.AcMaxChargingCurrent = (inputBuffer[18]) | (inputBuffer[19]<<8);
  2515. }
  2516. if((inputBuffer[9]>>3)&0x01)
  2517. {
  2518. SysConfigTemporarily.MaxChargingEnergy = (inputBuffer[10]) | (inputBuffer[11]<<8);
  2519. }
  2520. if((inputBuffer[9]>>2)&0x01)
  2521. {
  2522. SysConfigTemporarily.MaxChargingPower = (inputBuffer[12]) | (inputBuffer[13]<<8);
  2523. }
  2524. if((inputBuffer[9]>>1)&0x01)
  2525. {
  2526. SysConfigTemporarily.MaxChargingCurrent = (inputBuffer[14]) | (inputBuffer[15]<<8);
  2527. }
  2528. if(inputBuffer[9]&0x01)
  2529. {
  2530. SysConfigTemporarily.MaxChargingDuration = (inputBuffer[16]) | (inputBuffer[17]<<8);
  2531. }
  2532. tx_size = 7;
  2533. outBuffer[0] = 0xff;
  2534. outBuffer[1] = 0xff;
  2535. outBuffer[2] = 0x00;
  2536. outBuffer[3] = 0x03;
  2537. outBuffer[4] = WIFI_CMD_SET_EVSE_CONFIG;
  2538. outBuffer[5] = 0x01;
  2539. outBuffer[6] = chksumCal(outBuffer);
  2540. break;
  2541. case WIFI_CMD_SET_CHARGING_START:
  2542. tx_size = 7;
  2543. outBuffer[0] = 0xff;
  2544. outBuffer[1] = 0xff;
  2545. outBuffer[2] = 0x00;
  2546. outBuffer[3] = 0x03;
  2547. outBuffer[4] = WIFI_CMD_SET_CHARGING_START;
  2548. outBuffer[5] = (startConnectorCharging(inputBuffer[9])?0x01:0x00);
  2549. outBuffer[6] = chksumCal(outBuffer);
  2550. break;
  2551. case WIFI_CMD_SET_CHARGING_STOP:
  2552. tx_size = 7;
  2553. outBuffer[0] = 0xff;
  2554. outBuffer[1] = 0xff;
  2555. outBuffer[2] = 0x00;
  2556. outBuffer[3] = 0x03;
  2557. outBuffer[4] = WIFI_CMD_SET_CHARGING_STOP;
  2558. outBuffer[5] = (stopConnectorCharging(inputBuffer[9])?0x01:0x00);
  2559. outBuffer[6] = chksumCal(outBuffer);
  2560. break;
  2561. case WIFI_CMD_SET_SCHEDULE:
  2562. setConnectorSchedule(inputBuffer[9], inputBuffer);
  2563. tx_size = 7;
  2564. outBuffer[0] = 0xff;
  2565. outBuffer[1] = 0xff;
  2566. outBuffer[2] = 0x00;
  2567. outBuffer[3] = 0x03;
  2568. outBuffer[4] = WIFI_CMD_SET_SCHEDULE;
  2569. outBuffer[5] = 0x01;
  2570. outBuffer[6] = chksumCal(outBuffer);
  2571. break;
  2572. case WIFI_CMD_SET_ETH_CONFIG:
  2573. SysConfigTemporarily.Eth0Interface.EthDhcpClient = inputBuffer[9]^1;
  2574. tx_size = 7;
  2575. outBuffer[0] = 0xff;
  2576. outBuffer[1] = 0xff;
  2577. outBuffer[2] = 0x00;
  2578. outBuffer[3] = 0x03;
  2579. outBuffer[4] = WIFI_CMD_SET_ETH_CONFIG;
  2580. outBuffer[5] = 0x01;
  2581. outBuffer[6] = chksumCal(outBuffer);
  2582. break;
  2583. case WIFI_CMD_SET_ETH_IP:
  2584. memset(SysConfigTemporarily.Eth0Interface.EthIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthIpAddress));
  2585. memcpy(SysConfigTemporarily.Eth0Interface.EthIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2586. tx_size = 7;
  2587. outBuffer[0] = 0xff;
  2588. outBuffer[1] = 0xff;
  2589. outBuffer[2] = 0x00;
  2590. outBuffer[3] = 0x03;
  2591. outBuffer[4] = WIFI_CMD_SET_ETH_IP;
  2592. outBuffer[5] = 0x01;
  2593. outBuffer[6] = chksumCal(outBuffer);
  2594. break;
  2595. case WIFI_CMD_SET_ETH_SUBMASK:
  2596. memset(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress));
  2597. memcpy(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2598. tx_size = 7;
  2599. outBuffer[0] = 0xff;
  2600. outBuffer[1] = 0xff;
  2601. outBuffer[2] = 0x00;
  2602. outBuffer[3] = 0x03;
  2603. outBuffer[4] = WIFI_CMD_SET_ETH_SUBMASK;
  2604. outBuffer[5] = 0x01;
  2605. outBuffer[6] = chksumCal(outBuffer);
  2606. break;
  2607. case WIFI_CMD_SET_ETH_GATEWAY:
  2608. memset(SysConfigTemporarily.Eth0Interface.EthGatewayAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthGatewayAddress));
  2609. memcpy(SysConfigTemporarily.Eth0Interface.EthGatewayAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2610. tx_size = 7;
  2611. outBuffer[0] = 0xff;
  2612. outBuffer[1] = 0xff;
  2613. outBuffer[2] = 0x00;
  2614. outBuffer[3] = 0x03;
  2615. outBuffer[4] = WIFI_CMD_SET_ETH_GATEWAY;
  2616. outBuffer[5] = 0x01;
  2617. outBuffer[6] = chksumCal(outBuffer);
  2618. break;
  2619. case WIFI_CMD_SET_WIFI_MODE:
  2620. SysConfigTemporarily.AthInterface.WifiMode = inputBuffer[9];
  2621. tx_size = 7;
  2622. outBuffer[0] = 0xff;
  2623. outBuffer[1] = 0xff;
  2624. outBuffer[2] = 0x00;
  2625. outBuffer[3] = 0x03;
  2626. outBuffer[4] = WIFI_CMD_SET_WIFI_MODE;
  2627. outBuffer[5] = 0x01;
  2628. outBuffer[6] = chksumCal(outBuffer);
  2629. break;
  2630. case WIFI_CMD_SET_WIFI_SSID:
  2631. memset(SysConfigTemporarily.AthInterface.WifiSsid, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiSsid));
  2632. memcpy(SysConfigTemporarily.AthInterface.WifiSsid, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2633. tx_size = 7;
  2634. outBuffer[0] = 0xff;
  2635. outBuffer[1] = 0xff;
  2636. outBuffer[2] = 0x00;
  2637. outBuffer[3] = 0x03;
  2638. outBuffer[4] = WIFI_CMD_SET_WIFI_SSID;
  2639. outBuffer[5] = 0x01;
  2640. outBuffer[6] = chksumCal(outBuffer);
  2641. break;
  2642. case WIFI_CMD_SET_WIFI_PASSWD:
  2643. memset(SysConfigTemporarily.AthInterface.WifiPassword, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiPassword));
  2644. memcpy(SysConfigTemporarily.AthInterface.WifiPassword, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2645. tx_size = 7;
  2646. outBuffer[0] = 0xff;
  2647. outBuffer[1] = 0xff;
  2648. outBuffer[2] = 0x00;
  2649. outBuffer[3] = 0x03;
  2650. outBuffer[4] = WIFI_CMD_SET_WIFI_PASSWD;
  2651. outBuffer[5] = 0x01;
  2652. outBuffer[6] = chksumCal(outBuffer);
  2653. break;
  2654. case WIFI_CMD_SET_WIFI_DHCP:
  2655. SysConfigTemporarily.AthInterface.WifiDhcpClient = inputBuffer[9]^1;
  2656. tx_size = 7;
  2657. outBuffer[0] = 0xff;
  2658. outBuffer[1] = 0xff;
  2659. outBuffer[2] = 0x00;
  2660. outBuffer[3] = 0x03;
  2661. outBuffer[4] = WIFI_CMD_SET_WIFI_DHCP;
  2662. outBuffer[5] = 0x01;
  2663. outBuffer[6] = chksumCal(outBuffer);
  2664. break;
  2665. case WIFI_CMD_SET_WIFI_IP:
  2666. memset(SysConfigTemporarily.AthInterface.WifiIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiIpAddress));
  2667. memcpy(SysConfigTemporarily.AthInterface.WifiIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2668. tx_size = 7;
  2669. outBuffer[0] = 0xff;
  2670. outBuffer[1] = 0xff;
  2671. outBuffer[2] = 0x00;
  2672. outBuffer[3] = 0x03;
  2673. outBuffer[4] = WIFI_CMD_SET_WIFI_IP;
  2674. outBuffer[5] = 0x01;
  2675. outBuffer[6] = chksumCal(outBuffer);
  2676. break;
  2677. case WIFI_CMD_SET_WIFI_NETMASK:
  2678. memset(SysConfigTemporarily.AthInterface.WifiSubmaskAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiSubmaskAddress));
  2679. memcpy(SysConfigTemporarily.AthInterface.WifiSubmaskAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2680. tx_size = 7;
  2681. outBuffer[0] = 0xff;
  2682. outBuffer[1] = 0xff;
  2683. outBuffer[2] = 0x00;
  2684. outBuffer[3] = 0x03;
  2685. outBuffer[4] = WIFI_CMD_SET_WIFI_NETMASK;
  2686. outBuffer[5] = 0x01;
  2687. outBuffer[6] = chksumCal(outBuffer);
  2688. break;
  2689. case WIFI_CMD_SET_WIFI_GATEWAY:
  2690. memset(SysConfigTemporarily.AthInterface.WifiGatewayAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiGatewayAddress));
  2691. memcpy(SysConfigTemporarily.AthInterface.WifiGatewayAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2692. tx_size = 7;
  2693. outBuffer[0] = 0xff;
  2694. outBuffer[1] = 0xff;
  2695. outBuffer[2] = 0x00;
  2696. outBuffer[3] = 0x03;
  2697. outBuffer[4] = WIFI_CMD_SET_WIFI_GATEWAY;
  2698. outBuffer[5] = 0x01;
  2699. outBuffer[6] = chksumCal(outBuffer);
  2700. break;
  2701. case WIFI_CMD_SET_TELE_APN:
  2702. memset(SysConfigTemporarily.TelecomInterface.TelcomApn, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomApn));
  2703. memcpy(SysConfigTemporarily.TelecomInterface.TelcomApn, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2704. tx_size = 7;
  2705. outBuffer[0] = 0xff;
  2706. outBuffer[1] = 0xff;
  2707. outBuffer[2] = 0x00;
  2708. outBuffer[3] = 0x03;
  2709. outBuffer[4] = WIFI_CMD_SET_TELE_APN;
  2710. outBuffer[5] = 0x01;
  2711. outBuffer[6] = chksumCal(outBuffer);
  2712. break;
  2713. case WIFI_CMD_SET_TELE_CHAP_ID:
  2714. memset(SysConfigTemporarily.TelecomInterface.TelcomChapPapId, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomChapPapId));
  2715. memcpy(SysConfigTemporarily.TelecomInterface.TelcomChapPapId, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2716. tx_size = 7;
  2717. outBuffer[0] = 0xff;
  2718. outBuffer[1] = 0xff;
  2719. outBuffer[2] = 0x00;
  2720. outBuffer[3] = 0x03;
  2721. outBuffer[4] = WIFI_CMD_SET_TELE_CHAP_ID;
  2722. outBuffer[5] = 0x01;
  2723. outBuffer[6] = chksumCal(outBuffer);
  2724. break;
  2725. case WIFI_CMD_SET_TELE_CHAP_PASSWD:
  2726. memset(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd));
  2727. memcpy(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2728. tx_size = 7;
  2729. outBuffer[0] = 0xff;
  2730. outBuffer[1] = 0xff;
  2731. outBuffer[2] = 0x00;
  2732. outBuffer[3] = 0x03;
  2733. outBuffer[4] = WIFI_CMD_SET_TELE_CHAP_PASSWD;
  2734. outBuffer[5] = 0x01;
  2735. outBuffer[6] = chksumCal(outBuffer);
  2736. break;
  2737. case WIFI_CMD_SET_TELE_IP:
  2738. memset(SysConfigTemporarily.TelecomInterface.TelcomIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomIpAddress));
  2739. memcpy(SysConfigTemporarily.TelecomInterface.TelcomIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2740. tx_size = 7;
  2741. outBuffer[0] = 0xff;
  2742. outBuffer[1] = 0xff;
  2743. outBuffer[2] = 0x00;
  2744. outBuffer[3] = 0x03;
  2745. outBuffer[4] = WIFI_CMD_SET_TELE_IP;
  2746. outBuffer[5] = 0x01;
  2747. outBuffer[6] = chksumCal(outBuffer);
  2748. break;
  2749. case WIFI_CMD_SET_CONFIRM:
  2750. // Copy SysConfigTemporarily to ShmSysConfigAndInfo->SysConfig
  2751. memcpy(&ShmSysConfigAndInfo->SysConfig,&SysConfigTemporarily, sizeof(struct SysConfigData));
  2752. tx_size = 7;
  2753. outBuffer[0] = 0xff;
  2754. outBuffer[1] = 0xff;
  2755. outBuffer[2] = 0x00;
  2756. outBuffer[3] = 0x03;
  2757. outBuffer[4] = WIFI_CMD_SET_CONFIRM;
  2758. outBuffer[5] = (StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig)!=0x01?0x00:0x01);
  2759. outBuffer[6] = chksumCal(outBuffer);
  2760. Sys_Flag.isPkill = ON;
  2761. break;
  2762. case WIFI_CMD_SET_DISCARD:
  2763. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  2764. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2765. tx_size = 7;
  2766. outBuffer[0] = 0xff;
  2767. outBuffer[1] = 0xff;
  2768. outBuffer[2] = 0x00;
  2769. outBuffer[3] = 0x03;
  2770. outBuffer[4] = WIFI_CMD_SET_DISCARD;
  2771. outBuffer[5] = 0x01;
  2772. outBuffer[6] = chksumCal(outBuffer);
  2773. break;
  2774. default:
  2775. tx_size = 6;
  2776. outBuffer[0] = 0xff;
  2777. outBuffer[1] = 0xff;
  2778. outBuffer[2] = 0x00;
  2779. outBuffer[3] = 0x02;
  2780. outBuffer[4] = WIFI_CMD_UNKNOW;
  2781. outBuffer[5] = chksumCal(outBuffer);
  2782. break;
  2783. }
  2784. }
  2785. else
  2786. {
  2787. tx_size = 6;
  2788. outBuffer[0] = 0xff;
  2789. outBuffer[1] = 0xff;
  2790. outBuffer[2] = 0x00;
  2791. outBuffer[3] = 0x02;
  2792. outBuffer[4] = inputBuffer[8];
  2793. outBuffer[5] = chksumCal(outBuffer);
  2794. }
  2795. send(clientSockFd, outBuffer, tx_size, 0);
  2796. }
  2797. if(read_size == 0)
  2798. {
  2799. DEBUG_INFO("Client disconnected.\r\n");
  2800. wifi_login_info.loginRole = ROLE_UNKNOWN;
  2801. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  2802. close(clientSockFd);
  2803. if(Sys_Flag.isPkill == ON)
  2804. {
  2805. Sys_Flag.isPkill = OFF;
  2806. shutdown(sockFd, SHUT_RDWR);
  2807. close(sockFd);
  2808. DEBUG_INFO("pKill Module_Wifi after set confirm...\r\n");
  2809. system("pkill Module_Wifi");
  2810. }
  2811. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  2812. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2813. fflush(stdout);
  2814. }
  2815. else if(read_size == -1)
  2816. {
  2817. DEBUG_ERROR("Socket recv failed.\r\n");
  2818. }
  2819. sleep(1);
  2820. }
  2821. return FAIL;
  2822. }
  2823. //==========================================
  2824. // Common routine
  2825. //==========================================
  2826. char* getTimeString(void)
  2827. {
  2828. char *result=malloc(21);
  2829. time_t timep;
  2830. struct tm *p;
  2831. time(&timep);
  2832. p=gmtime(&timep);
  2833. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
  2834. return result;
  2835. }
  2836. void trim(char *s)
  2837. {
  2838. int i=0, j, k, l=0;
  2839. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  2840. i++;
  2841. j = strlen(s)-1;
  2842. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  2843. j--;
  2844. if(i==0 && j==strlen(s)-1) { }
  2845. else if(i==0) s[j+1] = '\0';
  2846. else {
  2847. for(k=i; k<=j; k++) s[l++] = s[k];
  2848. s[l] = '\0';
  2849. }
  2850. }
  2851. int mystrcmp(char *p1,char *p2)
  2852. {
  2853. while(*p1==*p2)
  2854. {
  2855. if(*p1=='\0' || *p2=='\0')
  2856. break;
  2857. p1++;
  2858. p2++;
  2859. }
  2860. if(*p1=='\0' && *p2=='\0')
  2861. return(PASS);
  2862. else
  2863. return(FAIL);
  2864. }
  2865. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  2866. {
  2867. strncpy(dest, src + start, cnt);
  2868. dest[cnt] = 0;
  2869. }
  2870. void split(char **arr, char *str, const char *del)
  2871. {
  2872. char *s = strtok(str, del);
  2873. while(s != NULL)
  2874. {
  2875. *arr++ = s;
  2876. s = strtok(NULL, del);
  2877. }
  2878. }
  2879. int checkIP(void)
  2880. {
  2881. // DHCP or static setting
  2882. int isGetIP = FAIL;
  2883. FILE *fp;
  2884. char buf[512];
  2885. char tmp[512];
  2886. char cmd[512];
  2887. if(Wifi.ipType == IP_TYPE_DHCP) {
  2888. // Get IP address & net mask
  2889. strcpy(cmd, "ifconfig ");
  2890. strcat(cmd, Wifi.currentInterface);
  2891. fp = popen(cmd, "r");
  2892. if(fp == NULL)
  2893. isGetIP = FAIL;
  2894. else
  2895. {
  2896. while(fgets(buf, sizeof(buf), fp) != NULL)
  2897. {
  2898. if(strstr(buf, "addr:") > 0)
  2899. {
  2900. if(strstr(buf, "192.168.1.10") > 0)
  2901. {
  2902. DEBUG_INFO("IP cannot be same as default value of Ethernet...\n");
  2903. }
  2904. else
  2905. {
  2906. isGetIP = PASS;
  2907. cnt_getIP_Fail = 0;
  2908. }
  2909. sscanf(buf, "%*s%s", tmp);
  2910. substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  2911. sscanf(buf, "%*s%*s%*s%s", tmp);
  2912. substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  2913. }else {
  2914. }
  2915. }
  2916. }
  2917. if(isGetIP == FAIL && cnt_getIP_Fail<3) {
  2918. sprintf(cmd, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  2919. system(cmd);
  2920. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
  2921. {
  2922. sprintf(cmd, "/sbin/udhcpc -i %s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface);
  2923. system(cmd);
  2924. }
  2925. DEBUG_INFO("Sending DHCP request...\n");
  2926. cnt_getIP_Fail++;
  2927. sleep(5);
  2928. }
  2929. pclose(fp);
  2930. }else {
  2931. strcpy(cmd, "ifconfig ");
  2932. strcat(cmd, Wifi.currentInterface);
  2933. strcat(cmd, " ");
  2934. strcat(cmd, Wifi.staticIPAddr);
  2935. fp = popen(cmd, "r");
  2936. DEBUG_INFO("Setting static IP to %s ...\n", Wifi.staticIPAddr);
  2937. pclose(fp);
  2938. isGetIP = PASS;
  2939. }
  2940. cnt_getIP_Fail = 0;
  2941. return isGetIP;
  2942. }
  2943. int restartWPA(void)
  2944. {
  2945. int result = FAIL;
  2946. FILE *fp;
  2947. char buf[512];
  2948. char cmd[50];
  2949. // Get IP address & net mask
  2950. strcpy(cmd, "wpa_cli -i ");
  2951. strcat(cmd, Wifi.currentInterface);
  2952. strcat(cmd, " reconfigure");
  2953. fp = popen(cmd, "r");
  2954. DEBUG_INFO("WPA reconfigure %s ...\n", Wifi.currentInterface);
  2955. if(fp == NULL)
  2956. result = FAIL;
  2957. else
  2958. {
  2959. while(fgets(buf, sizeof(buf), fp) != NULL)
  2960. {
  2961. if(strstr(buf, "OK") > 0)
  2962. {
  2963. DEBUG_INFO("reconfigure OK.\n");
  2964. result = PASS;
  2965. }else {
  2966. DEBUG_INFO("reconfigure failed.\n");
  2967. result = FAIL;
  2968. }
  2969. }
  2970. }
  2971. pclose(fp);
  2972. if(result == PASS) {
  2973. strcpy(cmd, "wpa_cli -i ");
  2974. strcat(cmd, Wifi.currentInterface);
  2975. strcat(cmd, " reconfigure");
  2976. fp = popen(cmd, "r");
  2977. DEBUG_INFO("WPA reconnect %s ...\n", Wifi.currentInterface);
  2978. if(fp == NULL)
  2979. result = FAIL;
  2980. else{
  2981. while(fgets(buf, sizeof(buf), fp) != NULL)
  2982. {
  2983. if(strstr(buf, "OK") > 0){
  2984. DEBUG_INFO("reconnect OK.\n");
  2985. result = PASS;
  2986. }else {
  2987. DEBUG_INFO("reconnect failed.\n");
  2988. result = FAIL;
  2989. }
  2990. }
  2991. }
  2992. pclose(fp);
  2993. }
  2994. return result;
  2995. }
  2996. void proc_sta()
  2997. {
  2998. switch(Wifi_module_sts)
  2999. {
  3000. case STA_STATE_INIT:
  3001. dispReq.isShowed_IF_info = false;
  3002. // get info from shared memory
  3003. getParameters();
  3004. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3005. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3006. isSetWPA_OK = false;
  3007. // check interface
  3008. if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
  3009. {
  3010. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3011. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  3012. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  3013. DEBUG_INFO("=================[State 1]===================\n");
  3014. }
  3015. else
  3016. {
  3017. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  3018. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  3019. sleep(30);
  3020. }
  3021. break;
  3022. case STA_STATE_DEVICE_DETECT:
  3023. dispReq.isShowed_IP_info = false;
  3024. if(!isSetWPA_OK && (setWPAconf() == PASS))
  3025. {
  3026. isSetWPA_OK = true;
  3027. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  3028. sleep(5);
  3029. }
  3030. else if((isSetWPA_OK == true) && (isConnectAP() == PASS))
  3031. {
  3032. Wifi_module_sts = STA_STATE_AP_CONNECTED;
  3033. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  3034. DEBUG_INFO("=================[State 2]===================\n");
  3035. }
  3036. else
  3037. {
  3038. cnt_getAP_Fail++;
  3039. DEBUG_INFO("Fail to connect to the AP %d times...\n",cnt_getAP_Fail);
  3040. if(cnt_getAP_Fail>=3)
  3041. {
  3042. Wifi_module_sts = STA_STATE_INIT;
  3043. cnt_getAP_Fail = 0;
  3044. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  3045. DEBUG_INFO("=================[State 0]===================\n");
  3046. }
  3047. else
  3048. {
  3049. sleep(10);
  3050. }
  3051. }
  3052. break;
  3053. case STA_STATE_AP_CONNECTED:
  3054. getInterfaceInfo();
  3055. getLinkQuality();
  3056. DEBUG_INFO("Wifi quality: %d dBm\n", Wifi.rssi);
  3057. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = Wifi.rssi;
  3058. if(isReachableInternet() == PASS)
  3059. {
  3060. DEBUG_INFO("Wifi internet valid result: Pass\n");
  3061. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=0;
  3062. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=1;
  3063. cnt_pingDNS_Fail = 0;
  3064. sleep(30);
  3065. }
  3066. else
  3067. {
  3068. cnt_pingDNS_Fail++;
  3069. DEBUG_INFO("Wifi internet valid result: Fail %d time\n", cnt_pingDNS_Fail);
  3070. sleep(5);
  3071. }
  3072. if(cnt_pingDNS_Fail >= 3)
  3073. {
  3074. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3075. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3076. cnt_pingDNS_Fail = 0;
  3077. DEBUG_INFO("Ping DNS failed...");
  3078. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  3079. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  3080. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  3081. if(isFindInterface() == PASS)
  3082. {
  3083. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3084. getParameters();
  3085. isSetWPA_OK = false;
  3086. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  3087. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  3088. DEBUG_INFO("=================[State 1]===================\n");
  3089. }
  3090. else
  3091. {
  3092. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  3093. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  3094. Wifi_module_sts = STA_STATE_INIT;
  3095. DEBUG_INFO("=================[State 0]===================\n");
  3096. }
  3097. }
  3098. break;
  3099. default:
  3100. Wifi_module_sts = STA_STATE_INIT;
  3101. break;
  3102. }
  3103. }
  3104. void proc_ap()
  3105. {
  3106. switch(Wifi_module_sts)
  3107. {
  3108. case AP_STATE_INIT:
  3109. // get info from shared memory
  3110. getParameters();
  3111. isSetWPA_OK = false;
  3112. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3113. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3114. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3115. // check interface
  3116. if(isFindInterface() == PASS)
  3117. {
  3118. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3119. Wifi_module_sts = AP_STATE_DEVICE_DETECT;
  3120. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  3121. DEBUG_INFO("=================[State 1]===================\n");
  3122. }
  3123. else
  3124. {
  3125. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  3126. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  3127. sleep(30);
  3128. }
  3129. break;
  3130. case AP_STATE_DEVICE_DETECT:
  3131. dispReq.isShowed_IP_info = false;
  3132. if(!isSetWPA_OK && (setWPAconf() == PASS))
  3133. {
  3134. isSetWPA_OK = true;
  3135. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  3136. sleep(5);
  3137. }
  3138. else if(isSetWPA_OK && (isStartUpAP() == PASS))
  3139. {
  3140. Wifi_module_sts = AP_STATE_AP_START;
  3141. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3142. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  3143. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3144. DEBUG_INFO("=================[State 2]===================\n");
  3145. }
  3146. else
  3147. {
  3148. cnt_getAP_Fail++;
  3149. DEBUG_INFO("Fail to start up AP %d times...\n",cnt_getAP_Fail);
  3150. if(cnt_getAP_Fail>=3)
  3151. {
  3152. Wifi_module_sts = AP_STATE_INIT;
  3153. cnt_getAP_Fail = 0;
  3154. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  3155. DEBUG_INFO("=================[State 0]===================\n");
  3156. }
  3157. else
  3158. {
  3159. sleep(10);
  3160. }
  3161. }
  3162. break;
  3163. case AP_STATE_AP_START:
  3164. getInterfaceInfo();
  3165. if(isStartUpAP() == PASS)
  3166. {
  3167. DEBUG_INFO("Wifi AP start up valid result: Pass\n");
  3168. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3169. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  3170. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3171. cnt_pingDNS_Fail = 0;
  3172. sleep(30);
  3173. }
  3174. else
  3175. {
  3176. cnt_pingDNS_Fail++;
  3177. DEBUG_INFO("Wifi AP start up valid result: Fail %d time\n", cnt_pingDNS_Fail);
  3178. sleep(5);
  3179. }
  3180. if(cnt_pingDNS_Fail >= 3)
  3181. {
  3182. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3183. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3184. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3185. cnt_pingDNS_Fail = 0;
  3186. DEBUG_INFO("Wifi AP start up status failed...");
  3187. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  3188. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  3189. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  3190. if(isFindInterface() == PASS)
  3191. {
  3192. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3193. isSetWPA_OK = false;
  3194. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  3195. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  3196. DEBUG_INFO("=================[State 1]===================\n");
  3197. }
  3198. else
  3199. {
  3200. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  3201. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  3202. Wifi_module_sts = STA_STATE_INIT;
  3203. DEBUG_INFO("=================[State 0]===================\n");
  3204. }
  3205. }
  3206. break;
  3207. default:
  3208. Wifi_module_sts = AP_STATE_INIT;
  3209. break;
  3210. }
  3211. }
  3212. void proc_killConnection()
  3213. {
  3214. char cmdBuf[512];
  3215. // Initialization flags when network is disable mode
  3216. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3217. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3218. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3219. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  3220. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  3221. // Stop dhcp client or server
  3222. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  3223. system(cmdBuf);
  3224. sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
  3225. system(cmdBuf);
  3226. // Restart wpa_supplicant
  3227. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  3228. system(cmdBuf);
  3229. // Stop wifi AP
  3230. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  3231. system(cmdBuf);
  3232. // Clean share memory when network is disable mode
  3233. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  3234. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  3235. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  3236. }
  3237. //==========================================
  3238. // Main loop
  3239. //==========================================
  3240. int main(void)
  3241. {
  3242. pid_t pid;
  3243. if(InitShareMemory() == FAIL)
  3244. {
  3245. DEBUG_ERROR("InitShareMemory NG\n");
  3246. if(ShmStatusCodeData!=NULL)
  3247. {
  3248. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  3249. }
  3250. sleep(5);
  3251. return 0;
  3252. }
  3253. // Local database initial
  3254. if(DB_Open(localDb) != PASS)
  3255. {
  3256. DEBUG_ERROR("Puk pin list local db initial fail.\n");
  3257. }
  3258. // Wifi login info initial & PUK generate
  3259. wifi_login_info.loginRole = ROLE_UNKNOWN;
  3260. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  3261. memset(&wifi_login_info.puk[0], 0x00, ARRAY_SIZE(wifi_login_info.puk));
  3262. sprintf((char*)wifi_login_info.puk, "%08X", ~stm32crc((uint32_t*)&ShmSysConfigAndInfo->SysConfig.SystemId[0], (ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)>>2)));
  3263. DEBUG_INFO("System ID: %s\r\n", ShmSysConfigAndInfo->SysConfig.SystemId);
  3264. DEBUG_INFO("EVSE PUK: %s\r\n", wifi_login_info.puk);
  3265. // UDP socket server start
  3266. pid = fork();
  3267. if(pid == 0)
  3268. {
  3269. if(udpSocketServerStart() == FAIL)
  3270. {
  3271. DEBUG_ERROR("UDP socket server down.\r\n");
  3272. return 0;
  3273. }
  3274. }
  3275. // TCP socket server start
  3276. pid = fork();
  3277. if(pid == 0)
  3278. {
  3279. if(tcpSocketServerStart() == FAIL)
  3280. {
  3281. DEBUG_ERROR("TCP socket server down.\r\n");
  3282. return 0;
  3283. }
  3284. }
  3285. //=============================================
  3286. // Install WIFI module driver
  3287. //=============================================
  3288. #ifdef UBLOX
  3289. system("insmod /lib/modules/mlan.ko");
  3290. system("insmod /lib/modules/usb8801.ko");
  3291. sleep(5);
  3292. system("ifconfig mlan0 up");
  3293. system("ifconfig uap0 up");
  3294. #endif
  3295. #ifdef MT7601U
  3296. system("insmod /lib/modules/mt7601u.ko");
  3297. sleep(5);
  3298. system("ifconfig wlan0 up");
  3299. #endif
  3300. DEBUG_INFO("=================[State 0]===================\n");
  3301. for(;;)
  3302. {
  3303. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  3304. {
  3305. case WIFI_MODE_STA:
  3306. proc_sta();
  3307. break;
  3308. case WIFI_MODE_AP:
  3309. proc_ap();
  3310. break;
  3311. case WIFI_MODE_ADHOC:
  3312. break;
  3313. case WIFI_MODE_DISABLE:
  3314. default:
  3315. proc_killConnection();
  3316. sleep(30);
  3317. break;
  3318. }
  3319. }
  3320. return 0;
  3321. }