Skip to content

Commit

Permalink
Use correct types for C integer casts
Browse files Browse the repository at this point in the history
  • Loading branch information
lassilaiho committed Dec 20, 2023
1 parent 1ae5715 commit b0c3e91
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
26 changes: 12 additions & 14 deletions pkg/rclgo/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"crypto/rand"
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -349,7 +348,7 @@ func (n *Node) NewActionServer(
rclOpts := C.rcl_action_server_options_t{
allocator: *n.context.rcl_allocator_t,
result_timeout: C.rcl_duration_t{
nanoseconds: C.long(opts.ResultTimeout),
nanoseconds: C.int64_t(opts.ResultTimeout),
},
}
opts.GoalServiceQos.asCStruct(&rclOpts.goal_service_qos)
Expand Down Expand Up @@ -526,16 +525,15 @@ func (s *ActionServer) scheduleRemoval() {
func (s *ActionServer) expireGoals() error {
var numExpired C.size_t
expiredGoals := make([]C.rcl_action_goal_info_t, 10)
expiredGoalsHeader := (*reflect.SliceHeader)(unsafe.Pointer(&expiredGoals))
s.rclServerMu.Lock()
defer s.rclServerMu.Unlock()
s.goalsMu.Lock()
defer s.goalsMu.Unlock()
for {
rc := C.rcl_action_expire_goals(
&s.rclServer,
(*C.rcl_action_goal_info_t)(unsafe.Pointer(expiredGoalsHeader.Data)),
C.ulong(expiredGoalsHeader.Len),
unsafe.SliceData(expiredGoals),
C.size_t(len(expiredGoals)),
&numExpired,
)
if rc != C.RCL_RET_OK {
Expand Down Expand Up @@ -983,16 +981,16 @@ func (c *ActionClient) SendGoalRequest(ctx context.Context, request types.Messag
return resp, err
}

func (c *ActionClient) sendGoalRequest(req unsafe.Pointer) (C.long, error) {
var seqNum C.long
func (c *ActionClient) sendGoalRequest(req unsafe.Pointer) (C.int64_t, error) {
var seqNum C.int64_t
rc := C.rcl_action_send_goal_request(&c.rclClient, req, &seqNum)
if rc != C.RCL_RET_OK {
return 0, errorsCastC(rc, "failed to send goal request")
}
return seqNum, nil
}

func (c *ActionClient) takeGoalResponse(resp unsafe.Pointer) (C.long, interface{}, error) {
func (c *ActionClient) takeGoalResponse(resp unsafe.Pointer) (C.int64_t, interface{}, error) {
var header C.rmw_request_id_t
switch rc := C.rcl_action_take_goal_response(&c.rclClient, &header, resp); rc {
case C.RCL_RET_OK:
Expand All @@ -1017,16 +1015,16 @@ func (c *ActionClient) GetResult(ctx context.Context, goalID *types.GoalID) (typ
return resp, err
}

func (c *ActionClient) sendResultRequest(req unsafe.Pointer) (C.long, error) {
var seqNum C.long
func (c *ActionClient) sendResultRequest(req unsafe.Pointer) (C.int64_t, error) {
var seqNum C.int64_t
rc := C.rcl_action_send_result_request(&c.rclClient, req, &seqNum)
if rc != C.RCL_RET_OK {
return 0, errorsCastC(rc, "failed to send result request")
}
return seqNum, nil
}

func (c *ActionClient) takeResultResponse(resp unsafe.Pointer) (C.long, interface{}, error) {
func (c *ActionClient) takeResultResponse(resp unsafe.Pointer) (C.int64_t, interface{}, error) {
var header C.rmw_request_id_t
switch rc := C.rcl_action_take_result_response(&c.rclClient, &header, resp); rc {
case C.RCL_RET_OK:
Expand Down Expand Up @@ -1060,16 +1058,16 @@ func (c *ActionClient) CancelGoal(ctx context.Context, request types.Message) (t
return resp, err
}

func (c *ActionClient) sendCancelRequest(req unsafe.Pointer) (C.long, error) {
var seqNum C.long
func (c *ActionClient) sendCancelRequest(req unsafe.Pointer) (C.int64_t, error) {
var seqNum C.int64_t
rc := C.rcl_action_send_cancel_request(&c.rclClient, req, &seqNum)
if rc != C.RCL_RET_OK {
return 0, errorsCastC(rc, "failed to send cancel request")
}
return seqNum, nil
}

func (c *ActionClient) takeCancelResponse(resp unsafe.Pointer) (C.long, interface{}, error) {
func (c *ActionClient) takeCancelResponse(resp unsafe.Pointer) (C.int64_t, interface{}, error) {
var header C.rmw_request_id_t
switch rc := C.rcl_action_take_cancel_response(&c.rclClient, &header, resp); rc {
case C.RCL_RET_OK:
Expand Down
2 changes: 1 addition & 1 deletion pkg/rclgo/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func logNamed(level LogSeverity, name, msg string) error {
}
loc.file_name = C.CString(file)
defer C.free(unsafe.Pointer(loc.file_name))
loc.line_number = C.ulong(line)
loc.line_number = C.size_t(line)
}
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
Expand Down
8 changes: 4 additions & 4 deletions pkg/rclgo/qos.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ func NewDefaultServiceQosProfile() QosProfile {

func (p *QosProfile) asCStruct(dst *C.rmw_qos_profile_t) {
dst.history = uint32(p.History)
dst.depth = C.ulong(p.Depth)
dst.depth = C.size_t(p.Depth)
dst.reliability = uint32(p.Reliability)
dst.durability = uint32(p.Durability)
dst.deadline = C.rmw_time_t{nsec: C.ulong(p.Deadline)}
dst.lifespan = C.rmw_time_t{nsec: C.ulong(p.Lifespan)}
dst.deadline = C.rmw_time_t{nsec: C.uint64_t(p.Deadline)}
dst.lifespan = C.rmw_time_t{nsec: C.uint64_t(p.Lifespan)}
dst.liveliness = uint32(p.Liveliness)
dst.liveliness_lease_duration = C.rmw_time_t{nsec: C.ulong(p.LivelinessLeaseDuration)}
dst.liveliness_lease_duration = C.rmw_time_t{nsec: C.uint64_t(p.LivelinessLeaseDuration)}
dst.avoid_ros_namespace_conventions = C.bool(p.AvoidRosNamespaceConventions)
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/rclgo/rcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func (c *Context) NewTimer(timeout time.Duration, timer_callback func(*Timer)) (
timer.rcl_timer_t,
c.Clock().rcl_clock_t,
c.rcl_context_t,
(C.long)(timeout),
C.int64_t(timeout),
nil,
*c.rcl_allocator_t,
)
Expand Down Expand Up @@ -1072,16 +1072,16 @@ func (c *Client) Send(ctx context.Context, req types.Message) (types.Message, *S
return resp, nil, err
}

func (c *Client) sendRequest(req unsafe.Pointer) (C.long, error) {
var seqNum C.long
func (c *Client) sendRequest(req unsafe.Pointer) (C.int64_t, error) {
var seqNum C.int64_t
rc := C.rcl_send_request(c.rclClient, req, &seqNum)
if rc != C.RCL_RET_OK {
return 0, errorsCastC(rc, "failed to send request")
}
return seqNum, nil
}

func (c *Client) takeResponse(resp unsafe.Pointer) (C.long, interface{}, error) {
func (c *Client) takeResponse(resp unsafe.Pointer) (C.int64_t, interface{}, error) {
var header C.rmw_service_info_t
switch rc := C.rcl_take_response_with_info(c.rclClient, &header, resp); rc {
case C.RCL_RET_OK:
Expand All @@ -1105,22 +1105,22 @@ type sendResult struct {
}

type requestSenderTransport struct {
SendRequest func(unsafe.Pointer) (C.long, error)
TakeResponse func(unsafe.Pointer) (C.long, interface{}, error)
SendRequest func(unsafe.Pointer) (C.int64_t, error)
TakeResponse func(unsafe.Pointer) (C.int64_t, interface{}, error)
TypeSupport types.ServiceTypeSupport
Logger *Logger
}

type requestSender struct {
transport requestSenderTransport
pendingRequests map[C.long]chan *sendResult
pendingRequests map[C.int64_t]chan *sendResult
mutex sync.Mutex
}

func newRequestSender(transport requestSenderTransport) requestSender {
return requestSender{
transport: transport,
pendingRequests: make(map[C.long]chan *sendResult),
pendingRequests: make(map[C.int64_t]chan *sendResult),
}
}

Expand Down Expand Up @@ -1155,7 +1155,7 @@ func (s *requestSender) Send(ctx context.Context, req types.Message) (types.Mess
}
}

func (s *requestSender) addPendingRequest(req types.Message) (<-chan *sendResult, C.long, error) {
func (s *requestSender) addPendingRequest(req types.Message) (<-chan *sendResult, C.int64_t, error) {
ts := s.transport.TypeSupport.Request()
buf := ts.PrepareMemory()
defer ts.ReleaseMemory(buf)
Expand Down

0 comments on commit b0c3e91

Please sign in to comment.