-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
76 changed files
with
89,052 additions
and
908 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## Viam CPP-SDK Style Guide | ||
- general introduction, reference to google guide and C++ core guidelines | ||
|
||
### Deviation | ||
- exceptions | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ | |
|
||
add_subdirectory(dial) | ||
add_subdirectory(echo) | ||
add_subdirectory(modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <common/v1/common.pb.h> | ||
#include <grpcpp/channel.h> | ||
#include <grpcpp/client_context.h> | ||
#include <grpcpp/grpcpp.h> | ||
#include <grpcpp/support/status.h> | ||
#include <robot/v1/robot.grpc.pb.h> | ||
#include <robot/v1/robot.pb.h> | ||
#include <unistd.h> | ||
|
||
#include <boost/optional.hpp> | ||
#include <cstddef> | ||
#include <functional> | ||
#include <iostream> | ||
#include <memory> | ||
#include <ostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "robot/client.hpp" | ||
#include "robot/service.hpp" | ||
#include "rpc/dial.hpp" | ||
|
||
using viam::robot::v1::Status; | ||
using Viam::SDK::Credentials; | ||
using Viam::SDK::Options; | ||
|
||
int main() { | ||
const char* uri = "<your robot URI here>"; | ||
Viam::SDK::DialOptions dial_options = Viam::SDK::DialOptions(); | ||
std::string payload = "<your payload here>"; | ||
Credentials credentials(payload); | ||
dial_options.credentials = credentials; | ||
boost::optional<Viam::SDK::DialOptions> opts(dial_options); | ||
std::string address(uri); | ||
Options options = Options(1, opts); | ||
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options); | ||
robot->refresh(); | ||
std::vector<ResourceName>* resource_names = robot->resource_names(); | ||
ResourceName the_one_we_care_about = resource_names->at(0); | ||
for (ResourceName resource : *resource_names) { | ||
std::cout << "Resource name: " << resource.name() << resource.type() << resource.subtype() | ||
<< std::endl; | ||
} | ||
std::vector<Status> status_plural = robot->get_status(); | ||
std::cout << "Status plural len " << status_plural.size() << std::endl; | ||
for (Status s : status_plural) { | ||
std::cout << " Status! " << s.name().subtype() << std::endl; | ||
} | ||
|
||
std::vector<ResourceName> just_one = std::vector<ResourceName>(); | ||
just_one.push_back(the_one_we_care_about); | ||
std::vector<Status> status_singular = robot->get_status(just_one); | ||
std::cout << "Status singular len " << status_singular.size() << std::endl; | ||
for (Status s : status_singular) { | ||
std::cout << " Status! " << s.name().subtype() << std::endl; | ||
} | ||
|
||
robot->close(); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
examples/echo/example_echo.cc → examples/echo/example_echo.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2023 Viam Inc. | ||
# | ||
# Licensed 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. | ||
|
||
add_executable(example_module | ||
example_module.cpp | ||
) | ||
|
||
target_link_libraries(example_module | ||
viamcpp | ||
) | ||
|
||
install( | ||
TARGETS example_module | ||
COMPONENT examples | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <component/arm/v1/arm.grpc.pb.h> | ||
#include <component/generic/v1/generic.grpc.pb.h> | ||
#include <grpcpp/grpcpp.h> | ||
#include <grpcpp/server_context.h> | ||
#include <robot/v1/robot.pb.h> | ||
#include <service/slam/v1/slam.grpc.pb.h> | ||
#include <signal.h> | ||
|
||
#include <components/component_base.hpp> | ||
#include <iostream> | ||
#include <memory> | ||
#include <module/module.hpp> | ||
#include <module/service.hpp> | ||
#include <rpc/dial.hpp> | ||
#include <rpc/server.hpp> | ||
|
||
#include "components/generic.hpp" | ||
#include "config/resource.hpp" | ||
#include "registry/registry.hpp" | ||
#include "resource/resource.hpp" | ||
|
||
using viam::component::generic::v1::GenericService; | ||
|
||
class MyModule : public GenericService::Service, public ComponentBase { | ||
public: | ||
void signal_handler(int signum); | ||
static int which; | ||
int inner_which; | ||
MyModule() { | ||
inner_which = which; | ||
which += 1; | ||
}; | ||
|
||
MyModule(const MyModule&) = delete; | ||
MyModule& operator=(const MyModule&) = delete; | ||
|
||
::grpc::Status DoCommand(::grpc::ServerContext* context, | ||
const ::viam::component::generic::v1::DoCommandRequest* request, | ||
::viam::component::generic::v1::DoCommandResponse* response) override { | ||
std::cout << "Received DoCommand request for MyModule number " << inner_which << std::endl; | ||
for (auto& req : request->command().fields()) { | ||
std::cout << "request key: " << req.first.c_str() | ||
<< "\trequest value: " << req.second.SerializeAsString(); | ||
} | ||
*response->mutable_result() = request->command(); | ||
|
||
return grpc::Status(); | ||
} | ||
}; | ||
|
||
int MyModule::which = 0; | ||
std::shared_ptr<ModuleService_> my_mod; | ||
|
||
void signal_handler(int signum) { | ||
my_mod->close(); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
if (argc != 2) { | ||
throw "need socket path as command line argument"; | ||
} | ||
|
||
// TODO(RSDK-1920) This is still causing non-graceful shutdown. Figure out why, and fix. | ||
struct sigaction sig_handler; | ||
sig_handler.sa_handler = signal_handler; | ||
sigaction(SIGTERM, &sig_handler, nullptr); | ||
sigemptyset(&sig_handler.sa_mask); | ||
sig_handler.sa_flags = 0; | ||
|
||
Subtype generic = Generic::subtype(); | ||
my_mod = std::make_shared<ModuleService_>(argv[1]); | ||
Model m("acme", "demo", "printer"); | ||
std::shared_ptr<ComponentRegistration> cr = std::make_shared<ComponentRegistration>( | ||
ComponentType("MyModule"), generic, m, [](std::string, std::shared_ptr<grpc::Channel>) { | ||
return std::make_unique<MyModule>(); | ||
}); | ||
|
||
Registry::register_component(cr); | ||
my_mod->add_model_from_registry(generic, m); | ||
|
||
my_mod->start(); | ||
Server::start(); | ||
Server::wait(); | ||
return 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.