summaryrefslogtreecommitdiffstatshomepage
path: root/cc3200/serverstask.c
blob: fbcc211a03dbc7714b3ae8c36b13ad851e96f01d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Daniel Campora
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdint.h>
#include <std.h>

#include "py/mpconfig.h"
#include MICROPY_HAL_H
#include "py/misc.h"
#include "simplelink.h"
#include "serverstask.h"
#include "modwlan.h"
#include "debug.h"
#include "mpexception.h"
#include "telnet.h"
#include "ftp.h"
#include "pybwdt.h"


/******************************************************************************
 DECLARE PRIVATE DEFINITIONS
 ******************************************************************************/

#define SERVERS_DEF_USER            "micro"
#define SERVERS_DEF_PASS            "python"

/******************************************************************************
 DEFINE PRIVATE TYPES
 ******************************************************************************/
typedef struct {
    volatile bool enabled;
    volatile bool do_disable;
    volatile bool do_enable;
}servers_Data_t;

/******************************************************************************
 DECLARE PRIVATE DATA
 ******************************************************************************/
static servers_Data_t servers_data = {.enabled = false, .do_disable = false, .do_enable = false};

/******************************************************************************
 DECLARE PRIVATE FUNCTIONS
 ******************************************************************************/

/******************************************************************************
 DECLARE PUBLIC DATA
 ******************************************************************************/
char *servers_user;
char *servers_pass;

/******************************************************************************
 DECLARE PUBLIC FUNCTIONS
 ******************************************************************************/

void TASK_Servers (void *pvParameters) {

    bool cycle = false;

    ASSERT ((servers_user = mem_Malloc(SERVERS_USER_LEN_MAX + 1)) != NULL);
    ASSERT ((servers_pass = mem_Malloc(SERVERS_PASS_LEN_MAX + 1)) != NULL);
    strcpy (servers_user, SERVERS_DEF_USER);
    strcpy (servers_pass, SERVERS_DEF_PASS);

    telnet_init();
    ftp_init();

    for ( ;; ) {

        if (servers_data.enabled) {
            if (servers_data.do_disable) {
                servers_data.do_disable = false;
                // disable all net processes
                telnet_disable();
                ftp_disable();

                // now clear the flag
                servers_data.enabled = false;
            }
            else {
                if (cycle) {
                    telnet_run();
                }
                else {
                    ftp_run();
                }
            }
        }
        else if (servers_data.do_enable) {
            servers_data.do_enable = false;

            telnet_enable();
            ftp_enable();

            // now set the flag
            servers_data.enabled = true;
        }

        cycle = cycle ? false : true;
        HAL_Delay(SERVERS_CYCLE_TIME_MS);
        // set the alive flag for the wdt
        pybwdt_srv_alive();
    }
}

void servers_start (void) {
    servers_data.do_disable = false;
    servers_data.do_enable = true;
}

void servers_stop (void) {
    servers_data.do_enable = false;
    servers_data.do_disable = true;
    do {
        HAL_Delay (SERVERS_CYCLE_TIME_MS);
    } while (servers_are_enabled());
}

bool servers_are_enabled (void) {
    return servers_data.enabled;
}

void servers_close_socket (int16_t *sd) {
    if (*sd > 0) {
        sl_Close(*sd);
        *sd = -1;
    }
}

void servers_set_login (char *user, char *pass) {
    memcpy(servers_user, user, SERVERS_USER_LEN_MAX);
    memcpy(servers_pass, pass, SERVERS_PASS_LEN_MAX);
}

/******************************************************************************
 DEFINE PRIVATE FUNCTIONS
 ******************************************************************************/