network.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <sys/timer.h>
  7. #include <sys/confnet.h>
  8. #include <sys/socket.h>
  9. #include <netinet/tcp.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <net/route.h>
  14. #include <dev/nicrtl.h>
  15. #include <stdio.h>
  16. #include <io.h>
  17. #include <arpa/inet.h>
  18. #include <pro/dhcp.h>
  19. #include <pro/sntp.h>
  20. #include "ntp.h"
  21. #include "network.h"
  22. #include "jsmn.h"
  23. #include "rtc.h"
  24. #include "alarm.h"
  25. bool isReceiving;
  26. bool hasNetwork;
  27. void NetworkInit() {
  28. hasNetwork = false;
  29. /* Register de internet controller. */
  30. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  31. printf("Registering failed. \n");
  32. }/* Netwerk configureren op dhcp */
  33. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  34. printf("DHCP failed. \n");
  35. }else {
  36. printf("Ik heb een internet connectie. Ip is: %s \nMac address is: %s\n\n", inet_ntoa(confnet.cdn_ip_addr), ether_ntoa(confnet.cdn_mac));
  37. }
  38. NutSleep(2000);
  39. hasNetwork = true;
  40. }
  41. char* getMacAdress(){
  42. ether_ntoa(confnet.cdn_mac);
  43. }
  44. char* httpGet(char address[]){
  45. u_long rx_to = 3000;
  46. isReceiving = true;
  47. printf("\n\n #-- HTTP get -- #\n");
  48. TCPSOCKET* sock = NutTcpCreateSocket();
  49. char buffer[2];
  50. char* content = (char*) calloc(1 , 800);
  51. char enters = 0;
  52. int t = 0;
  53. if(content == 0){
  54. printf("Can't calloc memory\n");
  55. }else if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  56. printf("Can't connect to server\n");
  57. }else if (NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to))){
  58. }else{
  59. FILE *stream;
  60. stream = _fdopen((int) sock, "r+b");
  61. //Writing http GET to stream
  62. fprintf(stream, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  63. fflush(stream);
  64. printf("Headers writed. Now reading.");
  65. NutDelay(500);
  66. //Removing header:
  67. while(fgets(buffer, sizeof(buffer), stream) != NULL) {
  68. if(enters == 4) {
  69. content[t] = buffer[0];
  70. t++;
  71. }else {
  72. if (buffer[0] == '\n' || buffer[0] == '\r') {
  73. enters++;
  74. }
  75. else {
  76. enters = 0;
  77. }
  78. }
  79. }
  80. fclose(stream);
  81. }
  82. NutTcpCloseSocket(sock);
  83. content[t] = '\0';
  84. printf("\nContent size: %d, Content: %s \n", t, content);
  85. isReceiving = false;
  86. return content;
  87. }
  88. int getTimeZone()
  89. {
  90. char* content = httpGet("/gettimezone.php");
  91. int timezone = atoi(content);
  92. free(content);
  93. printf("%d", timezone);
  94. return timezone;
  95. }
  96. void parseAlarmJson(char* content){
  97. int r;
  98. int i;
  99. jsmn_parser p;
  100. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  101. jsmn_init(&p);
  102. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  103. if (r <= 0) {
  104. printf("Failed to parse JSON: %d \n", r);
  105. return;
  106. }else{
  107. printf("Aantal tokens found: %d \n", r);
  108. }
  109. int start = 1;
  110. int usedAlarms[maxAlarms()];
  111. int j;
  112. for(j = 0; j < maxAlarms(); j++){
  113. usedAlarms[j] = 0;
  114. }
  115. for(i = 1; i < r; i++)
  116. {
  117. struct _tm time = GetRTCTime();
  118. u_short port;
  119. char url[24];
  120. char ip[24];
  121. char name[16];
  122. char st;
  123. memset(url, 0, 24);
  124. memset(ip, 0, 24);
  125. memset(name, 0, 16);
  126. int id;
  127. for (i = i; !((i + start) % 26 == 0); i++) {
  128. if (jsoneq(content, &token[i], "YYYY") == 0) {
  129. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  130. i++;
  131. }else if (jsoneq(content, &token[i], "MM") == 0) {
  132. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  133. i++;
  134. }else if (jsoneq(content, &token[i], "DD") == 0) {
  135. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  136. i++;
  137. }else if (jsoneq(content, &token[i], "hh") == 0) {
  138. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  139. i++;
  140. }else if (jsoneq(content, &token[i], "mm") == 0) {
  141. time.tm_min = getIntegerToken(content, &token[i + 1]);
  142. i++;
  143. }else if (jsoneq(content, &token[i], "ss") == 0) {
  144. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  145. i++;
  146. }else if (jsoneq(content, &token[i], "id") == 0) {
  147. id = getIntegerToken(content, &token[i + 1]);
  148. i++;
  149. }else if (jsoneq(content, &token[i], "port") == 0) {
  150. port = getIntegerToken(content, &token[i + 1]);
  151. i++;
  152. }else if (jsoneq(content, &token[i], "ip") == 0) {
  153. getStringToken(content, &token[i + 1], ip);
  154. i++;
  155. }else if (jsoneq(content, &token[i], "url") == 0) {
  156. getStringToken(content, &token[i + 1], url);
  157. i++;
  158. }else if (jsoneq(content, &token[i], "name") == 0) {
  159. getStringToken(content, &token[i + 1], name);
  160. i++;
  161. }else if (jsoneq(content, &token[i], "st") == 0) {
  162. st = getIntegerToken(content, &token[i + 1]);
  163. i++;
  164. }
  165. }
  166. start = 0;
  167. int idx = alarmExist(id);
  168. if(idx == -1){
  169. printf("New alarm found!\n");
  170. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  171. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  172. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  173. printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
  174. //zoek naar een vrije plaats in de alarm array
  175. for(j = 0; j < maxAlarms(); j++){
  176. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  177. setAlarm(time, name, ip, port, url, st, id, j);
  178. usedAlarms[j] = 1;
  179. j = 10;
  180. }
  181. }
  182. }else{
  183. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  184. }
  185. }
  186. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  187. if(usedAlarms[j] == 0){
  188. deleteAlarm(j);
  189. };
  190. }
  191. }
  192. bool NetworkIsReceiving(void){
  193. return isReceiving;
  194. }
  195. bool hasNetworkConnection(void){
  196. return hasNetwork;
  197. }