Project

General

Profile

Bug #11592 ยป OpENUtil.rb

ally0620 (Ally Kuo), 10/14/2015 03:41 AM

 
1

    
2
#
3
# (C) Copyright Broadcom Corporation 2012-2014
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
class OpENUtil
20
	attr_accessor :client
21

    
22
  def initialize()
23
    @client = nil
24
  end
25

    
26
  def connect(appname)
27
    maxtries = 100
28
    handle = OpEN::OpenapiClientHandle_s.new
29
    ret = OpEN.openapiClientRegister(appname, handle)
30
    @client = handle
31
    if ret == OpEN::OPEN_E_NONE
32
      ret = OpEN.openapiConnectivityCheck(@client)
33
      count = 0
34
      while ret != OpEN::OPEN_E_NONE and count < maxtries
35
        count+=1
36
        sleep(1)
37
        ret = OpEN.openapiConnectivityCheck(@client)
38
      end
39
    end
40
    return ret
41
  end
42

    
43
  def getCharBuffer(size, init=nil)
44
    ret = OpEN::CharArray.new(size)
45
    if init != nil
46
      if init.length > size
47
        count = size
48
      else
49
        count = init.length
50
      end
51
      for x in 0..count-1
52
        ret[x] = init[x]
53
      end
54
      ret[count] = nil
55
    else
56
      for x in 0..size-1
57
        ret[x] = nil
58
      end
59
    end
60
    return ret
61
  end
62

    
63
  def getUCharBuffer(size, init=nil)
64
    ret = OpEN::UcharArray.new(size)
65
    if init != nil
66
      if init.length > size
67
        count = size
68
      else
69
        count = init.length
70
      end      
71
      for x in 0..count-1
72
        if init[x] != nil
73
          ret[x] = init[x]
74
        end
75
      end
76
      ret[count] = 0
77
    else
78
      for x in 0..size-1
79
        ret[x] = 0
80
      end
81
    end
82
    return ret
83
  end
84

    
85
  def getIntBuffer(size)
86
    ret = OpEN::IntArray.new(size)
87
    return ret
88
  end
89

    
90
  def getNetworkOSVersion()
91
    size = 100
92
    switch_os_revision_string = getCharBuffer(size, nil)
93
    switch_os_revision = OpEN::Open_buffdesc.new
94
    switch_os_revision.pstart = switch_os_revision_string
95
    switch_os_revision.size = size 
96
    ret = OpEN::openapiNetworkOSVersionGet(@client, switch_os_revision)
97
    if ret != OpEN::OPEN_E_NONE
98
      puts "Network OS version retrieve error"
99
    else
100
      printf "ICOS Version %s\n", switch_os_revision_string.cast()
101
    end
102
  end
103

    
104
  def getAPIVersion()
105
    revData = OpEN::Open_revision_data_t.new
106
    ret = OpEN::openapiApiVersionGet(@client, revData)
107
    if ret != OpEN::OPEN_E_NONE
108
      puts "OpEN version retrieve error"
109
    else
110
      printf "OpEN version = %u.%u.%u.%u\n", revData.release, revData.version, revData.maint_level, revData.build_num
111
    end
112
  end
113

    
114
  def terminate()
115
  end
116
end