blob: 3f0e104829988453ecf60e4d52ec584b56e28ad7 [file]
/**
* 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 cli
import (
"fmt"
"mynewt.apache.org/newt/newtmgr/config"
"mynewt.apache.org/newt/newtmgr/protocol"
"mynewt.apache.org/newt/newtmgr/transport"
"github.com/spf13/cobra"
)
func echoRunCmd(cmd *cobra.Command, args []string) {
cpm, err := config.NewConnProfileMgr()
if err != nil {
nmUsage(cmd, err)
}
profile, err := cpm.GetConnProfile(ConnProfileName)
if err != nil {
nmUsage(cmd, err)
}
conn, err := transport.NewConn(profile)
if err != nil {
nmUsage(cmd, err)
}
runner, err := protocol.NewCmdRunner(conn)
if err != nil {
nmUsage(cmd, err)
}
echo, err := protocol.NewEcho()
if err != nil {
nmUsage(cmd, err)
}
echo.Message = args[0]
nmr, err := echo.EncodeWriteRequest()
if err != nil {
nmUsage(cmd, err)
}
if err := runner.WriteReq(nmr); err != nil {
nmUsage(cmd, err)
}
rsp, err := runner.ReadResp()
if err != nil {
nmUsage(cmd, err)
}
ersp, err := protocol.DecodeEchoResponse(rsp.Data)
if err != nil {
nmUsage(cmd, err)
}
fmt.Println(ersp.Message)
}
func echoCmd() *cobra.Command {
echoCmd := &cobra.Command{
Use: "echo",
Short: "Send data to remote endpoint using newtmgr, and receive data back",
Run: echoRunCmd,
}
return echoCmd
}