func()

in nmxact/nmserial/serial_xport.go [103:182]


func (sx *SerialXport) Start() error {
	if sx.port != nil {
		// Already started.
		return nil
	}

	c := &serial.Config{
		Name:        sx.cfg.DevPath,
		Baud:        sx.cfg.Baud,
		ReadTimeout: sx.cfg.ReadTimeout,
	}

	var err error
	sx.port, err = serial.OpenPort(c)
	if err != nil {
		return err
	}

	err = sx.port.Flush()
	if err != nil {
		return err
	}

	sx.wg.Add(1)
	go func() {
		defer sx.wg.Done()

		// Most of the reading will be done line by line, use the
		// bufio.Scanner to do this
		sx.scanner = bufio.NewScanner(sx.port)

		for {
			msg, err := sx.Rx()
			sx.Lock()
			if err != nil {
				if sx.rspSesn != nil {
					sx.rspSesn.errChan <- err
				}
			}
			if sx.closing {
				sx.Unlock()
				return
			}
			if msg == nil {
				sx.Unlock()
				continue
			}
			if len(msg) >= 4 {
				if sx.reqSesn != nil || sx.acceptSesn != nil {
					msgType := coap.COAPCode(msg[1])
					if msgType <= coap.DELETE {
						if sx.reqSesn != nil {
							sx.reqSesn.msgChan <- msg
							sx.Unlock()
							continue
						}
						if sx.acceptSesn != nil {
							s, err := sx.acceptServerSesn(
								sx.acceptSesn)
							if err != nil {
								log.Errorf("Cannot create server sesn: %v", err)
								sx.Unlock()
								continue
							}
							s.msgChan <- msg
							sx.Unlock()
							continue
						}
					}
				}
			}
			if sx.rspSesn != nil {
				sx.rspSesn.msgChan <- msg
			}
			sx.Unlock()
		}

	}()
	return nil
}