forked from apache/mynewt-newtmgr
/
udp_sesn.go
178 lines (147 loc) · 3.9 KB
/
udp_sesn.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package udp
import (
"fmt"
"net"
"time"
"github.com/runtimeco/go-coap"
"mynewt.apache.org/newtmgr/nmxact/mgmt"
"mynewt.apache.org/newtmgr/nmxact/nmcoap"
"mynewt.apache.org/newtmgr/nmxact/nmp"
"mynewt.apache.org/newtmgr/nmxact/nmxutil"
"mynewt.apache.org/newtmgr/nmxact/omp"
"mynewt.apache.org/newtmgr/nmxact/sesn"
)
type UdpSesn struct {
cfg sesn.SesnCfg
addr *net.UDPAddr
conn *net.UDPConn
txvr *mgmt.Transceiver
}
func NewUdpSesn(cfg sesn.SesnCfg) (*UdpSesn, error) {
s := &UdpSesn{
cfg: cfg,
}
txvr, err := mgmt.NewTransceiver(cfg.TxFilter, cfg.RxFilter, false,
cfg.MgmtProto, 3)
if err != nil {
return nil, err
}
s.txvr = txvr
return s, nil
}
func (s *UdpSesn) Open() error {
if s.conn != nil {
return nmxutil.NewSesnAlreadyOpenError(
"Attempt to open an already-open UDP session")
}
conn, addr, err := Listen(s.cfg.PeerSpec.Udp,
func(data []byte) {
s.txvr.DispatchNmpRsp(data)
})
if err != nil {
return err
}
s.addr = addr
s.conn = conn
return nil
}
func (s *UdpSesn) Close() error {
if s.conn == nil {
return nmxutil.NewSesnClosedError(
"Attempt to close an unopened UDP session")
}
s.conn.Close()
s.txvr.ErrorAll(fmt.Errorf("closed"))
s.txvr.Stop()
s.conn = nil
s.addr = nil
return nil
}
func (s *UdpSesn) IsOpen() bool {
return s.conn != nil
}
func (s *UdpSesn) MtuIn() int {
return MAX_PACKET_SIZE -
omp.OMP_MSG_OVERHEAD -
nmp.NMP_HDR_SIZE
}
func (s *UdpSesn) MtuOut() int {
return MAX_PACKET_SIZE -
omp.OMP_MSG_OVERHEAD -
nmp.NMP_HDR_SIZE
}
func (s *UdpSesn) TxRxMgmt(m *nmp.NmpMsg,
timeout time.Duration) (nmp.NmpRsp, error) {
if !s.IsOpen() {
return nil, fmt.Errorf("Attempt to transmit over closed UDP session")
}
txRaw := func(b []byte) error {
_, err := s.conn.WriteToUDP(b, s.addr)
return err
}
return s.txvr.TxRxMgmt(txRaw, m, s.MtuOut(), timeout)
}
func (s *UdpSesn) TxRxMgmtAsync(m *nmp.NmpMsg,
timeout time.Duration, ch chan nmp.NmpRsp, errc chan error) error {
rsp, err := s.TxRxMgmt(m, timeout)
if err != nil {
errc <- err
} else {
ch <- rsp
}
return nil
}
func (s *UdpSesn) AbortRx(seq uint8) error {
s.txvr.ErrorAll(fmt.Errorf("Rx aborted"))
return nil
}
func (s *UdpSesn) TxCoap(m coap.Message) error {
txRaw := func(b []byte) error {
_, err := s.conn.WriteToUDP(b, s.addr)
return err
}
return s.txvr.TxCoap(txRaw, m, s.MtuOut())
}
func (s *UdpSesn) MgmtProto() sesn.MgmtProto {
return s.cfg.MgmtProto
}
func (s *UdpSesn) ListenCoap(mc nmcoap.MsgCriteria) (*nmcoap.Listener, error) {
return s.txvr.ListenCoap(mc)
}
func (s *UdpSesn) StopListenCoap(mc nmcoap.MsgCriteria) {
s.txvr.StopListenCoap(mc)
}
func (s *UdpSesn) CoapIsTcp() bool {
return false
}
func (s *UdpSesn) RxAccept() (sesn.Sesn, *sesn.SesnCfg, error) {
return nil, nil, fmt.Errorf("Op not implemented yet")
}
func (s *UdpSesn) RxCoap(opt sesn.TxOptions) (coap.Message, error) {
return nil, fmt.Errorf("Op not implemented yet")
}
func (s *UdpSesn) Filters() (nmcoap.TxMsgFilter, nmcoap.RxMsgFilter) {
return s.txvr.Filters()
}
func (s *UdpSesn) SetFilters(txFilter nmcoap.TxMsgFilter,
rxFilter nmcoap.RxMsgFilter) {
s.txvr.SetFilters(txFilter, rxFilter)
}