2 #include "controller.h"
7 static char password[128];
8 static List *swiList=NULL;
9 static struct swi_attr *current=NULL;
10 static unsigned int seq=0;
15 // =============================================================================
18 // ------------------------
19 void startController (void) {
22 swiList=createEmptyList();
27 // -----------------------
28 void stopController (void) {
30 destroyList(swiList, free);
37 // =============================================================================
40 static void getSwitchAttributes (struct swi_attr *sa, const List *l) {
43 const struct attr *at;
47 memset(sa, 0, sizeof(struct swi_attr));
51 for (ln=l->first; ln!=NULL; ln=ln->next) {
57 len=min(at->size, PRODUCT_SIZE);
58 memcpy(sa->product, at->data, len);
59 trim(sa->product, len);
63 len=min(at->size, NAME_SIZE);
64 memcpy(sa->name, at->data, len);
69 memcpy(&sa->mac, at->data, ETH_ALEN);
73 sa->nc.ip=*(struct in_addr*)at->data;
77 sa->nc.netmask=*(struct in_addr*)at->data;
81 sa->nc.gw=*(struct in_addr*)at->data;
96 // ------------------------
97 int discoverSwitches (void) {
104 clearList(swiList, free);
107 attr=createEmptyList();
111 pushBackList(attr, newEmptyAttr(helloRequest[i]));
112 } while ( helloRequest[i++]!=ATTR_END );
114 i=sendNgPacket(CODE_READ_REQ, NULL, ++seq, attr);
115 destroyList(attr, (void(*)(void*))freeAttr);
121 while ( (attr=recvNgPacket (CODE_READ_REP, NULL, seq))!=NULL ) {
122 sa=malloc(sizeof(struct swi_attr));
123 getSwitchAttributes(sa, attr);
124 pushBackList(swiList, sa);
127 destroyList(attr, (void(*)(void*))freeAttr);
135 // -----------------------------
136 const List* getSwitchList (void) {
141 // ------------------------------------
142 const struct swi_attr* getCurrentSwitch (void) {
154 if ( i<0 || i>(int)swiList->count ) {
159 for (ln=swiList->first; i-->0; ln=ln->next);
163 //attr=createEmptyList();
175 // --------------------------------
176 void setPassword (const char *pass) {
178 strncpy(password, pass, sizeof(password));
183 //int changePassword (const char *pass);
187 // =============================================================================
190 //int getPortsNumber (void);
192 // ------------------------------------------------
193 int getPortsStatus (unsigned char *ports, int size) {
202 if ( ports==NULL || size<=0 ) {
204 } else if ( current==NULL ) {
209 attr=createEmptyList();
210 pushBackList(attr, newEmptyAttr(ATTR_PORT_STATUS));
211 pushBackList(attr, newEmptyAttr(ATTR_END));
214 i=sendNgPacket(CODE_READ_REQ, NULL, ++seq, attr);
215 destroyList(attr, (void(*)(void*))freeAttr);
220 if ( (attr=recvNgPacket (CODE_READ_REP, NULL, seq))==NULL ) {
224 for (ln=attr->first; ln!=NULL; ln=ln->next) {
227 if ( at->attr==ATTR_PORT_STATUS && p[0]-1<size ) {
233 destroyList(attr, (void(*)(void*))freeAttr);
241 //int getPortsStatistics (unsigned long long stats[][6], int size);
243 //int resetPortsStatistics (void);