Statistics
| Branch: | Tag: | Revision:

mongoose / examples / MSP432 / ccs / MSP432_MG_hello / main.c @ eaef5bd1

History | View | Annotate | Download (4.93 KB)

1
/*
2
 * Copyright (c) 2014-2016 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
/* Set up an AP or connect to existing WiFi network. */
7
#define WIFI_AP_SSID "Mongoose"
8
#define WIFI_AP_PASS ""
9
#define WIFI_AP_CHAN 6
10
// #define WIFI_STA_SSID "YourWiFi"
11
// #define WIFI_STA_PASS "YourPass"
12

    
13
#define MGOS_TASK_PRIORITY 3
14
#define MG_TASK_STACK_SIZE 8192
15

    
16
#include <stdbool.h>
17
#include <stdio.h>
18
#include <string.h>
19

    
20
/* XDCtools Header files */
21
#include <xdc/std.h>
22
#include <xdc/runtime/System.h>
23

    
24
/* BIOS Header files */
25
#include <ti/sysbios/BIOS.h>
26
#include <ti/sysbios/knl/Task.h>
27

    
28
/* TI-RTOS Header files */
29
#include <ti/drivers/GPIO.h>
30
#include <ti/drivers/WiFi.h>
31

    
32
/* Example/Board Header file */
33
#include "Board.h"
34

    
35
/* Mongoose.h brings in SimpleLink support. Do not include simplelink.h. */
36
#include <mongoose.h>
37

    
38
#include "cs_dbg.h"
39
#include "wifi.h"
40

    
41
static const char *upload_form =
42
    "\
43
<h1>Upload file</h1> \
44
<form action='/upload' method='POST' enctype='multipart/form-data'> \
45
  <input type='file' name='file'> \
46
  <input type='submit' value='Upload'> \
47
</form>";
48

    
49
static struct mg_str upload_fname(struct mg_connection *nc,
50
                                  struct mg_str fname) {
51
  struct mg_str lfn;
52
  lfn.len = fname.len + 3;
53
  lfn.p = malloc(lfn.len);
54
  memcpy((char *) lfn.p, "SL:", 3);
55
  memcpy((char *) lfn.p + 3, fname.p, fname.len);
56
  return lfn;
57
}
58

    
59
void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
60
  LOG(LL_DEBUG, ("%p ev %d", nc, ev));
61

    
62
  switch (ev) {
63
    case MG_EV_ACCEPT: {
64
      char addr[32];
65
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
66
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
67
      LOG(LL_INFO, ("Connection %p from %s", nc, addr));
68
      break;
69
    }
70
    case MG_EV_HTTP_REQUEST: {
71
      char addr[32];
72
      struct http_message *hm = (struct http_message *) ev_data;
73
      cs_stat_t st;
74
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
75
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
76
      LOG(LL_INFO,
77
          ("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
78
           hm->method.p, (int) hm->uri.len, hm->uri.p));
79
      if (mg_vcmp(&hm->uri, "/upload") == 0 ||
80
          (mg_vcmp(&hm->uri, "/") == 0 && mg_stat("SL:index.html", &st) != 0)) {
81
        mg_send(nc, upload_form, strlen(upload_form));
82
        nc->flags |= MG_F_SEND_AND_CLOSE;
83
        break;
84
      }
85
      struct mg_serve_http_opts opts;
86
      memset(&opts, 0, sizeof(opts));
87
      opts.document_root = "SL:";
88
      mg_serve_http(nc, hm, opts);
89
      break;
90
    }
91
    case MG_EV_CLOSE: {
92
      LOG(LL_INFO, ("Connection %p closed", nc));
93
      break;
94
    }
95
    case MG_EV_HTTP_PART_BEGIN:
96
    case MG_EV_HTTP_PART_DATA:
97
    case MG_EV_HTTP_PART_END: {
98
      struct mg_http_multipart_part *mp =
99
          (struct mg_http_multipart_part *) ev_data;
100
      if (ev == MG_EV_HTTP_PART_BEGIN) {
101
        LOG(LL_INFO, ("Begin file upload: %s", mp->file_name));
102
      } else if (ev == MG_EV_HTTP_PART_END) {
103
        LOG(LL_INFO, ("End file upload: %s", mp->file_name));
104
      }
105
      mg_file_upload_handler(nc, ev, ev_data, upload_fname);
106
    }
107
  }
108
}
109

    
110
static void mg_init(struct mg_mgr *mgr) {
111
  WiFi_Params wifiParams;
112
  WiFi_Handle handle;
113

    
114
  LOG(LL_INFO, ("MG task running"));
115

    
116
  /* Open WiFi driver */
117
  WiFi_Params_init(&wifiParams);
118
  wifiParams.bitRate = 2000000;
119
  handle = WiFi_open(Board_WIFI, Board_WIFI_SPI, NULL, &wifiParams);
120
  if (handle == NULL) {
121
    System_abort("WiFi driver failed to open.");
122
  }
123

    
124
  sl_Start(0, 0, 0);
125

    
126
  sl_fs_init();
127

    
128
#if defined(WIFI_STA_SSID)
129
  if (!wifi_setup_sta(WIFI_STA_SSID, WIFI_STA_PASS)) {
130
    LOG(LL_ERROR, ("Error setting up WiFi station"));
131
  }
132
#elif defined(WIFI_AP_SSID)
133
  if (!wifi_setup_ap(WIFI_AP_SSID, WIFI_AP_PASS, WIFI_AP_CHAN)) {
134
    LOG(LL_ERROR, ("Error setting up WiFi AP"));
135
  }
136
#else
137
#error WiFi not configured
138
#endif
139

    
140
  /* We don't need SimpleLink's web server. */
141
  sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID);
142

    
143
  const char *err = "";
144
  struct mg_bind_opts opts;
145
  memset(&opts, 0, sizeof(opts));
146
  opts.error_string = &err;
147

    
148
  struct mg_connection *nc = mg_bind(mgr, "80", mg_ev_handler);
149
  if (nc != NULL) {
150
    mg_set_protocol_http_websocket(nc);
151
  } else {
152
    LOG(LL_ERROR, ("Failed to create listener: %s", err));
153
  }
154
}
155

    
156
int main(void) {
157
  Board_initGeneral();
158
  Board_initGPIO();
159
  Board_initWiFi();
160

    
161
  setvbuf(stdout, NULL, _IOLBF, 0);
162
  setvbuf(stderr, NULL, _IOLBF, 0);
163
  cs_log_set_level(LL_INFO);
164
  cs_log_set_file(stdout);
165

    
166
  if (!mg_start_task(MGOS_TASK_PRIORITY, MG_TASK_STACK_SIZE, mg_init)) {
167
    LOG(LL_ERROR, ("Error starting Mongoose task"));
168
    return 1;
169
  }
170

    
171
  osi_start();
172

    
173
  return 0;
174
}
175

    
176
void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *e,
177
                                  SlHttpServerResponse_t *resp) {
178
}
179

    
180
void SimpleLinkSockEventHandler(SlSockEvent_t *e) {
181
}
182

    
183
void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *e) {
184
  LOG(LL_ERROR, ("status %d sender %d", e->EventData.deviceEvent.status,
185
                 e->EventData.deviceEvent.sender));
186
}