• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (C) 2023 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <android-base/strings.h>
20 #include <gflags/gflags.h>
21 #include <grpcpp/ext/proto_server_reflection_plugin.h>
22 #include <grpcpp/grpcpp.h>
23 #include <grpcpp/health_check_service_interface.h>
24 #include <sys/msg.h>
25 #include <unistd.h>
26 
27 #include <algorithm>
28 #include <array>
29 #include <iostream>
30 #include <memory>
31 #include <string>
32 
33 #include "wmediumd.grpc.pb.h"
34 #include "wmediumd/api.h"
35 #include "wmediumd/grpc.h"
36 
37 using google::protobuf::Empty;
38 using grpc::Server;
39 using grpc::ServerBuilder;
40 using grpc::ServerContext;
41 using grpc::Status;
42 using grpc::StatusCode;
43 using wmediumdserver::SetPositionRequest;
44 using wmediumdserver::WmediumdService;
45 
46 #define MAC_ADDR_LEN 6
47 #define STR_MAC_ADDR_LEN 17
48 
49 template <class T>
AppendBinaryRepresentation(std::string & buf,const T & data)50 static void AppendBinaryRepresentation(std::string& buf, const T& data) {
51   std::copy(reinterpret_cast<const char*>(&data),
52             reinterpret_cast<const char*>(&data) + sizeof(T),
53             std::back_inserter(buf));
54 }
55 
IsValidMacAddr(const std::string & mac_address)56 bool IsValidMacAddr(const std::string& mac_address) {
57   if (mac_address.size() != STR_MAC_ADDR_LEN) {
58     return false;
59   }
60 
61   if (mac_address[2] != ':' || mac_address[5] != ':' || mac_address[8] != ':' ||
62       mac_address[11] != ':' || mac_address[14] != ':') {
63     return false;
64   }
65 
66   for (int i = 0; i < STR_MAC_ADDR_LEN; ++i) {
67     if ((i - 2) % 3 == 0) continue;
68     char c = mac_address[i];
69 
70     if (isupper(c)) {
71       c = tolower(c);
72     }
73 
74     if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) return false;
75   }
76 
77   return true;
78 }
79 
ParseMacAddress(const std::string & mac_address)80 static std::array<uint8_t, 6> ParseMacAddress(const std::string& mac_address) {
81   auto split_mac = android::base::Split(mac_address, ":");
82   std::array<uint8_t, 6> mac;
83   for (int i = 0; i < 6; i++) {
84     char* end_ptr;
85     mac[i] = (uint8_t)strtol(split_mac[i].c_str(), &end_ptr, 16);
86   }
87 
88   return mac;
89 }
90 
91 class WmediumdServiceImpl final : public WmediumdService::Service {
92  public:
WmediumdServiceImpl(int event_fd,int msq_id)93   WmediumdServiceImpl(int event_fd, int msq_id)
94       : event_fd_(event_fd), msq_id_(msq_id) {}
95 
SetPosition(ServerContext * context,const SetPositionRequest * request,Empty * reply)96   Status SetPosition(ServerContext* context, const SetPositionRequest* request,
97                      Empty* reply) override {
98     // Validate parameters
99     if (!IsValidMacAddr(request->mac_address())) {
100       return Status(StatusCode::INVALID_ARGUMENT, "Got invalid mac address");
101     }
102     auto mac = ParseMacAddress(request->mac_address());
103 
104     // Construct request data
105     struct wmediumd_set_position data;
106     memcpy(data.mac, &mac, sizeof(mac));
107     data.x = request->x_pos();
108     data.y = request->y_pos();
109 
110     // Fill data in the message queue
111     struct wmediumd_grpc_message msg;
112     msg.type = GRPC_REQUEST;
113     memcpy(msg.data, &data, sizeof(data));
114     msgsnd(msq_id_, &msg, sizeof(data), 0);
115 
116     // Throw an event to wmediumd
117     uint64_t value = REQUEST_SET_POSITION;
118     write(event_fd_, &value, sizeof(uint64_t));
119 
120     return Status::OK;
121   }
122 
123  private:
124   int event_fd_;
125   int msq_id_;
126 };
127 
RunWmediumdServer(std::string grpc_uds_path,int event_fd,int msq_id)128 void RunWmediumdServer(std::string grpc_uds_path, int event_fd, int msq_id) {
129   std::string server_address("unix:" + grpc_uds_path);
130   WmediumdServiceImpl service(event_fd, msq_id);
131 
132   grpc::EnableDefaultHealthCheckService(true);
133   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
134   ServerBuilder builder;
135   // Listen on the given address without any authentication mechanism.
136   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
137   // Register "service" as the instance through which we'll communicate with
138   // clients. In this case it corresponds to an *synchronous* service.
139   builder.RegisterService(&service);
140   // Finally assemble the server.
141   std::unique_ptr<Server> server(builder.BuildAndStart());
142   std::cout << "Server listening on " << server_address << std::endl;
143 
144   // Wait for the server to shutdown. Note that some other thread must be
145   // responsible for shutting down the server for this call to ever return.
146   server->Wait();
147 }
148