]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/handlers.cpp
f0f3a8c1d097996927ea6eb9408f64dcb179797f
[stg.git] / projects / stargazer / plugins / other / smux / handlers.cpp
1 #include "asn1/OpenPDU.h"
2 #include "asn1/ClosePDU.h"
3 #include "asn1/RReqPDU.h"
4 #include "asn1/GetRequest-PDU.h"
5 #include "asn1/GetResponse-PDU.h"
6 #include "asn1/VarBindList.h"
7 #include "asn1/VarBind.h"
8 #include "asn1/OBJECT_IDENTIFIER.h"
9 #include "asn1/ber_decoder.h"
10 #include "asn1/der_encoder.h"
11
12 #include "stg/common.h"
13
14 #include "smux.h"
15
16 bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus)
17 {
18 printfd(__FILE__, "SMUX::CloseHandler()\n");
19 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
20 return false;
21 }
22
23 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
24 {
25 printfd(__FILE__, "SMUX::RegisterResponseHandler()\n");
26 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
27 return false;
28 }
29
30 bool SMUX::PDUsHandler(const SMUX_PDUs_t * pdus)
31 {
32 printfd(__FILE__, "SMUX::PDUsHandler()\n");
33 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
34 PDUsHandlers::iterator it;
35 it = pdusHandlers.find(pdus->choice.pdus.present);
36 if (it != pdusHandlers.end())
37     {
38     return (this->*(it->second))(&pdus->choice.pdus);
39     }
40 else
41     {
42     switch (pdus->present)
43         {
44         case PDUs_PR_NOTHING:
45             printfd(__FILE__, "SMUX::PDUsHandler() - nothing\n");
46             break;
47         case PDUs_PR_get_response:
48             printfd(__FILE__, "SMUX::PDUsHandler() - get response\n");
49             break;
50         case PDUs_PR_trap:
51             printfd(__FILE__, "SMUX::PDUsHandler() - trap\n");
52             break;
53         default:
54             printfd(__FILE__, "SMUX::PDUsHandler() - undefined\n");
55         }
56     }
57 return false;
58 }
59
60 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
61 {
62 printfd(__FILE__, "SMUX::CommitOrRollbackHandler()\n");
63 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
64 return false;
65 }
66
67 bool SMUX::GetRequestHandler(const PDUs_t * pdus)
68 {
69 printfd(__FILE__, "SMUX::GetRequestHandler()\n");
70 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
71 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
72 GetResponse_PDU_t msg;
73 VarBindList_t * varBindList = &msg.variable_bindings;
74 memset(&msg, 0, sizeof(msg));
75
76 msg.request_id = getRequest->request_id;
77 asn_long2INTEGER(&msg.error_status, 0);
78 asn_long2INTEGER(&msg.error_index, 0);
79
80 const VarBindList_t * vbl = &getRequest->variable_bindings; 
81 for (int i = 0; i < vbl->list.count; ++i)
82     {
83     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
84     Sensors::iterator it;
85     it = sensors.find(OI2String(&vb->name));
86     if (it == sensors.end())
87         {
88         SendGetResponseErrorPDU(sock, getRequest,
89                                 PDU__error_status_noSuchName, i);
90         return true;
91         }
92
93     VarBind_t newVb;
94     memset(&newVb, 0, sizeof(newVb));
95
96     newVb.name = vb->name;
97     it->second->GetValue(&newVb.value);
98
99     ASN_SEQUENCE_ADD(varBindList, &newVb);
100     }
101
102 SendGetResponsePDU(sock, &msg);
103 asn_fprint(stderr, &asn_DEF_PDU, &msg);
104 return false;
105 }
106
107 bool SMUX::GetNextRequestHandler(const PDUs_t * pdus)
108 {
109 printfd(__FILE__, "SMUX::GetNextRequestHandler()\n");
110 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
111 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
112 GetResponse_PDU_t msg;
113 VarBindList_t * varBindList = &msg.variable_bindings;
114 memset(&msg, 0, sizeof(msg));
115
116 msg.request_id = getRequest->request_id;
117 asn_long2INTEGER(&msg.error_status, 0);
118 asn_long2INTEGER(&msg.error_index, 0);
119
120 const VarBindList_t * vbl = &getRequest->variable_bindings; 
121 for (int i = 0; i < vbl->list.count; ++i)
122     {
123     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
124     Sensors::iterator it;
125     it = sensors.upper_bound(OI2String(&vb->name));
126     if (it == sensors.end())
127         {
128         SendGetResponseErrorPDU(sock, getRequest,
129                                 PDU__error_status_noSuchName, i);
130         return true;
131         }
132
133     VarBind_t newVb;
134     memset(&newVb, 0, sizeof(newVb));
135
136     newVb.name = vb->name;
137     it->second->GetValue(&newVb.value);
138
139     ASN_SEQUENCE_ADD(varBindList, &newVb);
140     }
141
142 SendGetResponsePDU(sock, &msg);
143 asn_fprint(stderr, &asn_DEF_PDU, &msg);
144 return false;
145 }
146
147 bool SMUX::SetRequestHandler(const PDUs_t * pdus)
148 {
149 printfd(__FILE__, "SMUX::SetRequestHandler()\n");
150 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
151 SendGetResponseErrorPDU(sock, &pdus->choice.set_request,
152                         PDU__error_status_readOnly, 0);
153 return false;
154 }