network.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <sys/types.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <net/route.h>
  15. #include <dev/nicrtl.h>
  16. #include <stdio.h>
  17. #include <io.h>
  18. #include <sys/version.h>
  19. #include <dev/irqreg.h>
  20. #include <arpa/inet.h>
  21. #include <pro/dhcp.h>
  22. #include <pro/sntp.h>
  23. #include "ntp.h"
  24. #include "network.h"
  25. #include "jsmn.h"
  26. #include "rtc.h"
  27. #include "alarm.h"
  28. #include "contentparser.h"
  29. bool isReceiving;
  30. bool hasNetwork;
  31. void NetworkInit() {
  32. hasNetwork = false;
  33. /* Register de internet controller. */
  34. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  35. printf("Registering failed. \n");
  36. }/* Netwerk configureren op dhcp */
  37. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  38. printf("DHCP failed. \n");
  39. }else {
  40. 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));
  41. }
  42. NutSleep(2000);
  43. hasNetwork = true;
  44. }
  45. char* getMacAdress(){
  46. return ether_ntoa(confnet.cdn_mac);
  47. }
  48. void httpGet(char address[], void (*parser)(char*)){
  49. u_long rx_to = 10000;
  50. isReceiving = true;
  51. printf("\n\n #-- HTTP get -- #\n");
  52. TCPSOCKET* sock = NutTcpCreateSocket();
  53. char buffer[2];
  54. char* content = (char*) malloc(900);
  55. char enters = 0;
  56. int t = 0;
  57. if(content == 0){
  58. printf("Can't malloc memory\n");
  59. }else if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  60. printf("Can't connect to server\n");
  61. }else if (NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to))){
  62. printf("Can't set sock options\n");
  63. }else{
  64. FILE *stream;
  65. stream = _fdopen((int) sock, "r+b");
  66. //Writing http GET to stream
  67. fprintf(stream, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  68. fflush(stream);
  69. printf("Headers writed. Now reading.");
  70. //Removing header:
  71. while(fgets(buffer, sizeof(buffer), stream) != NULL) {
  72. if(enters == 4) {
  73. content[t] = buffer[0];
  74. t++;
  75. }else {
  76. if (buffer[0] == '\n' || buffer[0] == '\r') {
  77. enters++;
  78. }
  79. else {
  80. enters = 0;
  81. }
  82. }
  83. }
  84. fclose(stream);
  85. content[t] = '\0';
  86. printf("\nContent size: %d, Content: %s \n", t, content);
  87. parser(content);
  88. }
  89. free(content);
  90. NutTcpCloseSocket(sock);
  91. isReceiving = false;
  92. }
  93. bool NetworkIsReceiving(void){
  94. return isReceiving;
  95. }
  96. bool hasNetworkConnection(void){
  97. return hasNetwork;
  98. }